Calling RESTful services from your Android app

Mobile developer William J. Francis demonstrates how easy it is to consume a RESTful service from your Android device.

RESTful services are well suited for providing content to small footprint devices like smartphones and tablets. In fact, if you've interacted with any cloud-based APIs in the last couple of years, there is a strong chance that API was exposed via a REST interface. There are a number of documents available on the web explaining what constitutes a REST service and how to implement one. There are also a number of write-ups explaining how to consume REST services from a client. Yet a common request I see in Android forums is for examples of Java-based Android specific REST consumers.

Recently my son and I released an update for our game, which included an online leaderboard. The server code is pretty simple -- it keeps track of the five best scores on each of the 40 levels. Because of its light-weight and relative ease of implementation, I exposed the leaderboard to my app (and anyone else who is interested) via a RESTful HTTP GET call. For instance to see the top five players on level #1, just pop this URL into any browser:

Figure A

Calling a REST endpoint from within an Android application requires pushing an HTTP request to the background thread and then parsing the results on the UI thread. It's not very difficult once you see it laid out in front of you, but getting started can sometimes be a bit of a hurdle.

This tutorial includes a pared down version of the code actually used in my production application. Hopefully it will prove useful to you in getting started with your own Android REST consumers. You can follow along with the step-by-step tutorial, or download the entire project to import into Eclipse.

1. Create a new Android project in Eclipse. Target SDK 1.6 or greater, and be sure to rename the startup activity to

2. In your /res/layout folder, create a main.xml resource that will define a button and an edit box.


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""



android:orientation="vertical" >




android:text="Http GET Demo"/>





android:text="GET" android:id="@+id/my_button"/> <EditText android:layout_margin="20dip"







android:id="@+id/my_edit"/> </LinearLayout>
3. Because our application will be making HTTP calls, we will have to declare the uses internet permission in the manifest.

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android=""



android:versionName="1.0" >

<uses-sdk android:minSdkVersion="4" />

<uses-permission android:name="android.permission.INTERNET"/>



android:label="@string/app_name" >



android:label="@string/app_name" >


<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />




4. The file in our /src directory contains the standard on create override and implements an on click listener for the button.
package com.authorwjf.http_get;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity implements OnClickListener {


public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);





public void onClick(View arg0) {

Button b = (Button)findViewById(;

b.setClickable(false); new LongRunningGetIO().execute();



5. Our file references something called LongRunningGetIO; this is something we need to implement as well, and for the sake of our example, we will do so as an inner class inside our Main. The inner class extends AsyncTask. If you aren't familiar with how it works, you can get up to speed by reading my post on Long Running I/O.
private class LongRunningGetIO extends AsyncTask <Void, Void, String> {
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {

InputStream in = entity.getContent();

StringBuffer out = new StringBuffer(); int n = 1; while (n>0) { byte[] b = new byte[4096];

n =;

if (n>0) out.append(new String(b, 0, n));


return out.toString();



protected String doInBackground(Void... params) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet(""); String text = null; try {

HttpResponse response = httpClient.execute(httpGet, localContext);

HttpEntity entity = response.getEntity();

text = getASCIIContentFromEntity(entity);

} catch (Exception e) { return e.getLocalizedMessage();


return text;


protected void onPostExecute(String results) { if (results!=null) {

EditText et = (EditText)findViewById(;



Button b = (Button)findViewById(;




The demo should be ready for you to try. Upload the APK to an emulator and tap the button. Providing you have an Internet connection and the server isn't overloaded with requests, you should get something back. The demo doesn't do any parsing on the result string, but it should be clear how easy it would be to encompass the results in a JSON array and display them in a meaningful manner.

Figure B

Congratulations -- you've just consumed a RESTful service from your Android device!


William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...


thank u for tuto.i  dont know why i have always why same pb "Chunked stream ended unexpectedly" plz can u help  me.


Hi, I've been reading in other places and have bumped over the use of StringBuilder instead of StringBuffer for the getASCIIContentFromEntity() method. It says that the StringBuilder is faster than StringBuffer because it's unsynchronized. Since we're on an AsyncTask, do you think it would actually be better to use StringBuilder for this code?

Hoping for your insights.


Thanks ! I wonder if i want to convert HttpEntity to a List Object. May you show me how to do that ? Thanks


Hello William, 

Just want to know how to upload the data to same server with Android app.

Thanks in advance!!


Such a great tutorial. Thanks man


When I tried to follow this tutorial, I found that in onPostExecute, the section Button b = (Button)findViewById(; b.setClickable(true); resulted in an exception: "Only the original thread that created a view hierarchy can touch its views." When I googled this error message, it seems to be a documented limitation, but perhaps that is only a recently introcuced one?

Editor's Picks