Using the Http Client for Http Get Requests

Here's the general pattern for using the HttpClient:

1. Create an HttpClient (or get an existing reference).

2. Instantiate a new HTTP method, such as PostMethod or GetMethod.

3. Set HTTP parameter names/values.

4. Execute the HTTP call using the HttpClient.

5. Process the HTTP response.

Listing 8-1 shows how to execute an HTTP GET using the HttpClient.

Note Because the code attempts to use the Internet, you will need to add android.permission. INTERNET to your manifest file when making HTTP calls using the HttpClient.

Listing 8-1. Using the HttpClient to Get an HTTP GET request import; import; import; import;

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;

public class TestHttpGet {

public void executeHttpGet() throws Exception { BufferedReader in = null; try {

HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet();

request.setURI(new URI("")); HttpResponse response = client.execute(request); in = new BufferedReader (new InputStreamReader(response.getEntity() .getContent()));

StringBuffer sb = new StringBuffer(""); String line = "";

String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL);

String page = sb.toString(); System.out.println(page); } finally {

in.close(); } catch (IOException e) { e.printStackTrace();

The HttpClient provides abstractions for the various HTTP request types, such as HttpGet, HttpPost, and so on. Listing 8-1 uses the HttpClient to get the contents of the http://code. URL. The actual HTTP request is executed with the call to client. execute(). After executing the request, the code reads the entire response into a string object. Note that the BufferedReader is closed in the finally block, which also closes the underlying HTTP connection.

Realize that the class in Listing 8-1 does not extend In other words, you don't need to be within the context of an activity to use HttpClient—because HttpClient is packaged with Android, you can use it from within the context of any Android component (such as an activity) or use it as part of a standalone class.

The code in Listing 8-1 executes an HTTP request without passing any HTTP parameters to the server. You can pass name/value parameters as part of the request by appending name/ value pairs to the URL, as shown in Listing 8-2.

Listing 8-2. Adding Parameters to an HTTP GET Request

HttpGet method = new HttpGet("http://somehost/WS2/Upload.aspx?one=valueGoesHere"); client.execute(method);

When you execute an HTTP GET, the parameters (names and values) of the request are passed as part of the URL. Passing parameters this way has some limitations. Namely, the length of a URL should be kept below 2,048 characters. Instead of using HTTP GET, you can use HTTP POST. The POST method is more flexible and passes parameters as part of the request body.

0 0

Post a comment