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());只显示 this finalResult。那么这有什么问题:

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

???

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

字符串 responseBody = EntityUtils.toString(entity);。

请帮忙!!!

4

6 回答 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());
于 2012-05-21T11:18:00.457 回答
9

你可以使用这个:

String s = EntityUtils.toString(httpRes.getEntity());
于 2015-12-14T16:25:47.277 回答
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 作为参数传递。享受!!

于 2015-09-01T07:53:01.220 回答
1

试试这个:

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = "";
while ((body = rd.readLine()) != null) 
{
    Log.e("HttpResponse", body);
}
于 2012-05-21T11:15:28.890 回答
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");

            }
        });
    }
于 2013-09-15T10:23:14.170 回答
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();
于 2012-05-21T11:26:18.393 回答