Android REST: HTTP GET Example

In developing a mobile application on Google Android platform often requires client-server communications. The communication itself often uses TCP/IP protocol on port 80 (HTTP) to represent a client state to a (service) server. This communication style often known as Representational State Transfer (REST). REST is a style of software architecture for distributed systems such as the World Wide Web. REST has emerged as a predominant Web service design model (wikipedia). If REST implemented in HTTP, it is often called a RESTful Web API or RESTful Web Services.

The keys in using REST architecture are:

Below is an example on how to implement a RESTful client in Google Android java:


package com.ptiik.library;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Rest {
	private int timeout = 30000;
	private HttpClient httpClient;
	private HttpPost httpPost;
	private HttpGet httpGet;
	private HttpResponse httpResponse;

	private String error;
	private String responseString = null;

	private ArrayList data;

	public Rest(){
		this.httpClient = new DefaultHttpClient();
		this.data = new ArrayList();
	}

	public void setTimeout(int timeout){
		this.timeout = timeout;
	}

	public void post(String url){
		this.post(url, null);
	}

	public void post(String url, ArrayList data){
		this.httpPost = new HttpPost(url);
		this.httpClient.getParams().setParameter("http.socket.timeout", this.timeout);
		try {
			this.data = data;
			if(data != null)
				this.httpPost.setEntity(new UrlEncodedFormEntity(this.data));
			this.httpResponse = this.httpClient.execute(this.httpPost);
		} catch (UnsupportedEncodingException e) {
			this.error = e.getMessage();
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			this.error = e.getMessage();
			e.printStackTrace();
		} catch (IOException e) {
			this.error = e.getMessage();
			e.printStackTrace();
		}
	}

	public void get(String url){
		this.httpGet = new HttpGet(url);
		this.httpClient.getParams().setParameter("http.socket.timeout", this.timeout);
		try {
			this.httpResponse = this.httpClient.execute(this.httpGet);
		} catch (UnsupportedEncodingException e) {
			this.error = e.getMessage();
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			this.error = e.getMessage();
			e.printStackTrace();
		} catch (IOException e) {
			this.error = e.getMessage();
			e.printStackTrace();
		}
	}	

	public String getError() {
		return this.error;
	}

	public String getResponseString(){
		if(this.httpResponse!=null){
			try {
				HttpEntity httpEntity = this.httpResponse.getEntity();
				InputStream is = httpEntity.getContent();
	            BufferedReader reader = new BufferedReader(
	            	new InputStreamReader(is, "utf-8"), 8);
	            StringBuilder sb = new StringBuilder();
	            String line = null;
	            while ((line = reader.readLine()) != null) {
	                sb.append(line + "\n");
	            }
	            is.close();
	            this.responseString = sb.toString();
	            return this.responseString;
	        } catch (Exception e) {
	        	this.error = e.getMessage();
	            Log.e("pnote", "Response string buffer error. " + e.getMessage());
	        }
		} 
		return null;
	}

	public JSONObject getResponseJSONObject(){
		String JSONString = this.getResponseString();
		if(JSONString != null)
			Log.i("pnote", JSONString);
		else Log.i("pnote", "JSONString is null");
		if(JSONString != null) {
			JSONObject jObj;
			try {
				jObj = new JSONObject(JSONString);
				return jObj;
			} catch (JSONException e) {
				this.error = e.getMessage();
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getResponseText(){
		return this.responseString;
	}
}

The simplest usage of Rest class above are as follow:


Rest r = new Rest();
r.get('http://google.com');
r.getResponseString();
String textRetrieved = r.getResponseText();

Then the text (string) retrieved in textRetrieved variable are free to process on your Android mobile application.

The above codes are free for you to use and is licensed under LGPL license. If you have any questions, feel free to ask.