A web service wouldn't be a “web” service if we didn't use HTTP to access it. Making an HTTP request on Android is straightforward using the Apache-provided HttpClient
classes that are included with Android in the org.apache.http
package.
First we need to create an HttpClient
object, which will actually be a DefaultHttpClient
object.
HttpClient httpclient = new DefaultHttpClient();
Following that, we can construct the request. In this case, we'll be making an HTTP GET request, which puts any parameters on the URL in the form of a query string. This is as opposed to an HTTP POST request, which sends additional data as the body of the request message, which is not part of the URL.
To create an HTTP GET request, we'll instantiate an HttpGet
object, passing in a URL to the page that we would like to retrieve. In this case, we are passing in a URL to the page about this book on Apress's site.
HttpGet httpget = new HttpGet("http://www.apress.com/book/view/9781430232674");
We'll then execute the request by passing the HttpGet
object to the HttpClient
object via the execute
method. This will return an HttpResponse
object.
HttpResponse response = httpclient.execute(httpget);
The HttpResponse
will contain an HttpEntity
, which is basically an HTTP message. Both requests and responses contain entities.
HttpEntity entity = response.getEntity();
The getContent
method on the HttpEntity
returns an InputStream
that we can use to read the actual content that was sent in response.
InputStream inputstream = entity.getContent();
Let's go through a short example to illustrate.
package com.apress.proandroidmedia.ch12.simplehttprequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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 android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class SimpleHTTPRequest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
First we'll instantiate our HttpClient
and HttpGet
objects.
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.apress.com/book/view/9781430232674");
The execute
method on our HttpClient
may throw an exception, so we need to wrap it in a try
catch
block.
try {
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
If the HttpEntity
exists, we can get access to the InputStream
, which can be used to read the response.
InputStream inputstream = entity.getContent();
We'll wrap the InputStream
in a BufferedReader
and utilize a StringBuilder
object to turn it into a normal String
that we can work with in the end.
BufferedReader bufferedreader =
new BufferedReader(new InputStreamReader(inputstream));
StringBuilder stringbuilder = new StringBuilder();
String currentline = null;
try {
while ((currentline = bufferedreader.readLine()) != null) {
stringbuilder.append(currentline + "
");
}
} catch (IOException e) {
e.printStackTrace();
}
After fully reading the content, we use the toString
method on our StringBuilder
object to get the resulting String
, which will then print out via the Log.
String result = stringbuilder.toString();
Log.v("HTTP REQUEST",result);
inputstream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Of course, we'll need to have permission to access the Internet, so we'll need to specify that in our AndroidManifest.xml
.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Now that we know how to make an HTTP request for a generic web resource, let's look at how we can deal with the type of data that may be returned after a request to a web service. We'll start with JSON-formatted data.
3.144.244.250