Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

android - getting the response body of HttpResponse

I have done this:

response = httpclient.execute(targetHost, httppost); 
    if(response.getStatusLine().getStatusCode() == 200)
                        {
    HttpEntity entity = response.getEntity();
    System.out.println("Entity:"+entity);
  if (entity != null) 
                            {
        String responseBody = EntityUtils.toString(entity);
        System.out.println("finalResult"+responseBody.toString());
                            }

The thing about it is that the first println() displays this: org.apache.http.conn.BasicManagedEntity@481e8150 which is good.

But the second System.out.println("finalResult"+responseBody.toString()); displays only this finalResult. So what is wrong with this:

String responseBody = EntityUtils.toString(entity);
            System.out.println("finalResult"+responseBody.toString());

???

IMPORTANT This HttpEntity entity = response.getEntity(); is equal to org.apache.http.conn.BasicManagedEntity@481e8150. SO the problem must be here:

String responseBody = EntityUtils.toString(entity);.

Please help!!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, see if your server is not returning blank response:

response.getEntity().getContentLength();  //it should not be 0

Second, try the following to convert response into string:

StringBuilder sb = new StringBuilder();
try {
    BufferedReader reader = 
           new BufferedReader(new InputStreamReader(entity.getContent()), 65728);
    String line = null;

    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
}
catch (IOException e) { e.printStackTrace(); }
catch (Exception e) { e.printStackTrace(); }


System.out.println("finalResult " + sb.toString());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...