获取HttpResponse的响应体

11

我已经做过这件事:

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,这很好。

但是第二个System.out.println("finalResult"+responseBody.toString());仅显示了finalResult。那么这段代码有什么问题呢?

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

重要提示:这个HttpEntity entity = response.getEntity();等同于org.apache.http.conn.BasicManagedEntity@481e8150。所以问题一定出在这里:

String responseBody = EntityUtils.toString(entity);。

请帮忙!!!

6个回答

27

首先,检查您的服务器是否未返回空白响应:

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

其次,尝试以下方法将响应转换为字符串:

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

如果这样的话,那就意味着你的 Web 服务没有返回任何数据。 - waqaslam
1
这意味着你的对象不是空的,这很好,但这并不意味着你的对象内部包含数据。 - waqaslam
1
是的,它是0,这意味着您的服务器返回了空响应 :) - waqaslam
嘿,我正在使用相同的代码,但是出现了“尝试从关闭的流中读取android”异常。有人能帮我解决这个问题吗? - Salman Khan
@SalmanKhan请确保在流已被消耗和/或关闭后不再阅读或使用它。 - waqaslam
显示剩余3条评论

9
您可以使用这个:
String s = EntityUtils.toString(httpRes.getEntity());

3
 org.apache.http.conn.BasicManagedEntity@f8a5dec

当我们直接打印HttpEntity对象时,响应就会出现。例如:

 HttpEntity httpEntity=httpResponse.getEntity();

现在我们需要执行以下步骤来获取实际的服务器响应:
 public String convertStreamtoString(InputStream is){

    String line="";
    String data="";
    try{
        BufferedReader br=new BufferedReader(new InputStreamReader(is));
        while((line=br.readLine())!=null){

            data+=line;
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
        return  data;
}

只需调用上述方法并将httpEntity作为参数传递即可。享受吧!

1

试试这个:

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = "";
while ((body = rd.readLine()) != null) 
{
    Log.e("HttpResponse", body);
}

1
rd 是 java.io.BufferedReader@48073398。但是 body = rd.readLine(); 为 null! - adrian

1
尝试这个:

HttpEntity entity = response.getEntity();  
final String content;
    try
    {
        content = EntityUtils.toString(entity);

        runOnUiThread(new Runnable()
        {

            @Override
            public void run()
            {

                webView.loadData(content, "text/html", "UTF-8");

            }
        });
    }

0

试试这个

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

                //SB to make a string out of the inputstream
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();

                //the json string is stored here
            String  result = sb.toString();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接