11

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

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

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

???

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

String responseBody = EntityUtils.toString(entity);.

Please help!!!

adrian
  • 4,574
  • 17
  • 68
  • 119

6 Answers6

27

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());
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • then it means there's no data returned from your webservice – waqaslam May 21 '12 at 12:02
  • but when I display HttpEntity entity = response.getEntity(); System.out.println("finalResult " + entity); it shows org.apache.http.conn.BasicManagedEntity@48246b10. This means the response from webservice is not null, right? – adrian May 21 '12 at 12:07
  • try logging `response.getEntity().getContentLength();` and see if the content length is more than **0** otherwise you are getting nothing from server – waqaslam May 21 '12 at 12:08
  • 1
    It means your object is not null which is good, but then it doesn't mean that your object does contain data inside. – waqaslam May 21 '12 at 12:15
  • 1
    yes, its 0, so it means your server is returning blank response :) – waqaslam May 21 '12 at 12:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11583/discussion-between-adrian-and-waqas) – adrian May 22 '12 at 13:36
  • Hey, I am using the same code but getting Exception"attempted read from closed stream android". Can anyone help me out with this. – Salman Khan Feb 03 '16 at 06:15
  • @SalmanKhan make sure you are not reading/using the stream once it is consumed and/or closed. – waqaslam Feb 03 '16 at 12:38
9

You can use this one:

String s = EntityUtils.toString(httpRes.getEntity());
hany mhajna
  • 134
  • 1
  • 8
3
 org.apache.http.conn.BasicManagedEntity@f8a5dec

response come when we directly print HttpEntity object. eg:

 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!!

Aman Goel
  • 3,351
  • 1
  • 21
  • 17
1

Try this:

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = "";
while ((body = rd.readLine()) != null) 
{
    Log.e("HttpResponse", body);
}
Demonick
  • 2,116
  • 3
  • 30
  • 40
1

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

            }
        });
    }
Noman
  • 4,049
  • 10
  • 38
  • 59
0

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();
Khan
  • 7,585
  • 3
  • 27
  • 44