Calling a RESTful web service from an Android application

This a simple example of how you can call a RESTful web service from an Android application.
First of all, you need to request a permission to access network. This can be done by adding the following tag to your AndroidManifest.xml:

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

Then we can go to the actual implementation. The easiest way is to use Apache Http Client, which is bundled with Android:

The AsyncTask class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread.
You then can make a request by adding the following line to your code:

If you are going to access a service from your local machine, keep in mind that the localhost IP address for Android is 10.0.2.2 instead of 127.0.0.1.
After the execute method returns, you just have to call the following method to get the response: