a REST helper class for java

Java

Public Domain

Simple wrapper around HTTP GET and POST's to send and receive JSON data

Download (right click, save as, rename as appropriate)

Embed

Tags:

HTTP Java JSON REST
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
 
/**
 * Base class for REST Clients  
 */
public class RESTClient {
 
	/**
	 * Reads the contents of a stream into a string	 
	 */
    private String convertStreamToString(InputStream is) {       
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
            	throw new RuntimeException(e);
            }
        }
        
        return sb.toString();
    }
 
  
    
    /**
     * Makes a GET request to the specified url and returns a JSON Object
     * @param url
     * @return a JSON encoded response
     * @throws JSONException
     */
    protected JSONObject makeGETRequest(String url) throws JSONException {
    	HttpClient httpclient = new DefaultHttpClient();
    	HttpGet httpget = new HttpGet(url); 
    	HttpResponse response;
    	String resultText = "";
    	
    	try {
			response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			  if (entity != null) {
				  InputStream instream = null;
				  try {
					  instream = entity.getContent();
					  resultText = convertStreamToString(instream);
				  }
	              finally {
	            	  instream.close();  
	              }
			  }
			 
		} catch (ClientProtocolException e) {
			throw new RuntimeException(e);			
		} catch (IOException e) {
			throw new RuntimeException(e);			
		}
    	 
    	return new JSONObject(resultText);    	
    }  
    
    /**
     * Makes a POST request to the specified URL and passes the provided JSON Object
     * @param url URL Endpoint
     * @param contents JSON Object to post to URL
     * @return a JSON encoded response
     * @throws JSONException
     */
    protected JSONObject makePOSTRequest(String url, JSONObject contents) throws JSONException {
    	HttpClient httpclient = new DefaultHttpClient();
    	HttpPost post = new HttpPost(url);
    	
    	try {
			post.setEntity(new StringEntity(contents.toString()));
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}    	
    	
		HttpResponse response;
    	String resultText = "";
    	
    	try {
			response = httpclient.execute(post);
			HttpEntity entity = response.getEntity();
			  if (entity != null) {
				  InputStream instream = null;
				  try {
					  instream = entity.getContent();
					  resultText = convertStreamToString(instream);
				  }
	              finally {
	            	  instream.close();  
	              }
			  }
			 
		} catch (ClientProtocolException e) {
			throw new RuntimeException(e);			
		} catch (IOException e) {
			throw new RuntimeException(e);			
		}
    	 
    	return new JSONObject(resultText);    
    	
    }
}