Android 获取 HttpResponse 的响应体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10684086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:36:40  来源:igfitidea点击:

getting the response body of HttpResponse

androidhttpentity

提问by adrian

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@481e8150which is good.

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

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

但是第二个System.out.println("finalResult"+responseBody.toString());只显示 this finalResult。那么这有什么问题:

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

???

???

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

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

String responseBody = EntityUtils.toString(entity);.

String responseBody = EntityUtils.toString(entity);。

Please help!!!

请帮忙!!!

回答by waqaslam

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

回答by hany mhajna

You can use this one:

你可以使用这个:

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

回答by Aman Goel

 org.apache.http.conn.BasicManagedEntity@f8a5dec

response come when we directly print HttpEntity object. eg:

当我们直接打印 HttpEntity 对象时响应来了。例如:

 HttpEntity httpEntity=httpResponse.getEntity();

Now for getting the actual Response from the server we need to do following steps:

现在要从服务器获取实际响应,我们需要执行以下步骤:

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

just call above method and pass httpEntity as an argument. Enjoy!!

只需调用上面的方法并将 httpEntity 作为参数传递。享受!!

回答by Noman

Try this :

尝试这个 :

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

            }
        });
    }

回答by Demonick

Try this:

尝试这个:

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

回答by Khan

try this

尝试这个

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