English 中文(简体)
正在获取 HttpResponse 响应主体
原标题:getting the response body of HttpResponse

我这样做了:

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 (“ 最终Result” +respondBody.toString () ) ; 只显示此 finalResult 。 那么这有什么错 :

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

?????

impportant this HttpEntity 实体=响应.getEntity (); 等于 org.apache.http.conn.BasicManagedEntity@481e8150 。 因此问题必须在这里 :

字符串响应Body = 实体Unitels.to string(实体);.

请救救我!

最佳回答

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

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());
问题回答

您可以使用这个:

String s = EntityUtils.toString(httpRes.getEntity());
 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;
}

请在方法之上呼唤,然后通过 httpEnity作为辩论。请慢用!

尝试此 :

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

尝试一下这个 :

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

            }
        });
    }

试这个

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签