29

I have this code on my Android phone.

   URI uri = new URI(url);
   HttpPost post = new HttpPost(uri);
   HttpClient client = new DefaultHttpClient();
   HttpResponse response = client.execute(post);

I have a asp.net webform application that has in the page load this

 Response.Output.Write("It worked");

I want to grab this Response from the HttpReponse and print it out. How do I do this?

I tried response.getEntity().toString() but it just seems to print out the address in memory.

Thanks

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
chobo2
  • 83,322
  • 195
  • 530
  • 832

5 Answers5

40

Use ResponseHandler. One line of code. See here and here for sample Android projects using it.

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/user");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler=new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost, responseHandler);
        JSONObject response=new JSONObject(responseBody);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

add combination of this post and complete HttpClient at - http://www.androidsnippets.org/snippets/36/

Dwhitz
  • 1,250
  • 7
  • 26
  • 38
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • which android api version does this become available at? I might have to rewrite come code :D –  Apr 04 '10 at 00:26
  • I think it's been there since the beginning, or at least since Android 0.9. It's part of the standard HttpClient 4.x package. – CommonsWare Apr 04 '10 at 00:32
12

I would just do it the old way. It's a more bulletproof than ResponseHandler, in case you get different content types in the response.

ByteArrayOutputStream outstream = new ByteArrayOutputStream();
response.getEntity().writeTo(outstream);
byte [] responseBody = outstream.toByteArray();
hnviet
  • 1,747
  • 1
  • 18
  • 22
8

I used the following code

BufferedReader r = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuilder total = new StringBuilder();

String line = null;

while ((line = r.readLine()) != null) {
   total.append(line);
}
r.close();
return total.toString();
Fopa Léon Constantin
  • 11,863
  • 8
  • 48
  • 82
Lunf
  • 442
  • 4
  • 9
8

The simplest approach is probably using org.apache.http.util.EntityUtils:

String message = EntityUtils.toString(response.getEntity());

It reads the contents of an entity and returns it as a String. The content is converted using the character set from the entity (if any), failing that, "ISO-8859-1" is used.

If necessary, you can pass a default character set explicitly - e.g.

String message = EntityUtils.toString(response.getEntity(). "UTF-8");

It gets the entity content as a String, using the provided default character set if none is found in the entity. If the passed default character set is null, the default "ISO-8859-1" is used.

JimiLoe
  • 950
  • 2
  • 14
  • 22
6

This code will return the entire response message in respond as a String, and status code in rsp, as an int.

respond = response.getStatusLine().getReasonPhrase();

rsp = response.getStatusLine().getStatusCode();`
pjco
  • 3,826
  • 25
  • 25
Sanghita
  • 1,307
  • 3
  • 16
  • 28