得到HttpResponse的响应主体

时间:2012-05-21 11:09:38

标签: android http entity

我做到了:

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 个答案:

答案 0 :(得分:24)

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

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

答案 1 :(得分:6)

你可以使用这个:

String s = EntityUtils.toString(httpRes.getEntity());

答案 2 :(得分: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作为参数传递。享受!!

答案 3 :(得分:1)

试试这个:

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

答案 4 :(得分: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");

            }
        });
    }

答案 5 :(得分: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();