Tuesday 6 December 2011

Android - Get body of HTTP Response as String


Making HTTP requests in Android often results in the same type of boiler plate code being produced. One in particular that annoys me is having to get the response body as in InputStream and using BufferedReader and the likes.


Bundled along with Android is the org.apache.http libraries, which provide classes for easy interaction for common HTTP-related activities.


The following snippet shows how to perform an HTTP GET on a URI, and turn the response body into a string. In my case, I take the string and turn into a JSON object, but you might parse as XML or whatever.


DefaultHttpClient http = new DefaultHttpClient();
HttpGet httpMethod = new HttpGet();
httpMethod.setURI(new URI(SERVER_URI_GOES_HERE));
HttpResponse response = http.execute(httpMethod);
int responseCode = response.getStatusLine().getStatusCode();
switch(responseCode)
{
    case 200:
        HttpEntity entity = response.getEntity();
if(entity != null)
{
String responseBody = EntityUtils.toString(entity);
                 
        }
        break;

5 comments:

  1. Nice code.
    I have been trying to apply your code to this website but am getting the whole source code for the website whereas I just need to identify the mobile number network operator. Please can you help me check the site and tell me how i can get the output I desire using your code?
    The url is http://www.siirretytnumerot.fi/index.html?clientLanguage=eng

    ReplyDelete
    Replies
    1. Getting the website back in HTML format is actually expected in this case. It doesn't look like the site is designed to return you anything else. In other words, what you are likely doing is performing a GET on the url, which is likely going to result in the server giving you content back as if you were a regular browser accessing that url (so you get back HTML).

      If you own the server, you can modify the response to give back different types to different clients, based on the client's Accept http header.

      If you don't own the site, then you will have to deal with parsing the HTML to get what you need I'm afraid.

      Delete
    2. Also to note, you might want to skip that url altogether and POST straight to http://www.siirretytnumerot.fi/QueryServlet. The page you mention will take you to a page containing a form; I'm guessing here but I imagine you want to skip the form and go straight to the results?

      You will need to post the parameters that would have gotten set using the form on the url you mention above.

      Delete
  2. is it fast enough to integrate on a working mysql based php web site

    ReplyDelete
    Replies
    1. The code uses a well used java library for reading an HTTP Response Body into a String. As for performance, it is fast and efficient. It isn't doing anything special here, it is just a convenience method to save you re-writing the underlying code yourself.

      If you are interested at seeing the underlying implementation, see http://hc.apache.org/httpcomponents-core-ga/httpcore/clover/org/apache/http/util/EntityUtils.html

      As for "mysql based php web site" - it doesn't matter what is used on the server-side; this article is only in regards to the client consuming from that server.

      Delete