得到的Htt presponse的响应正文 [英] getting the response body of HttpResponse

查看:242
本文介绍了得到的Htt presponse的响应正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做到了这一点:

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());
                            }

关于它的事情是,在第一个的println()显示这样的: org.apache.http.conn.BasicManagedEntity@481e8150 这是很好的。

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

但第二的System.out.println(finalResult+ responseBody.toString()); 只显示该 finalResult 。那么,什么是错的:

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());

???

重要:该 HttpEntity实体= response.getEntity(); 等于 org.apache.http.conn .BasicManagedEntity @ 481e8150 。所以这个问题必须在这里:

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

字符串responseBody = EntityUtils.toString(实体);

String responseBody = EntityUtils.toString(entity);.

请帮助!

推荐答案

首先,看看如果你的服务器没有返回无表情的反应:

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());

这篇关于得到的Htt presponse的响应正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆