6

The method below falls over when reading the HttpResponse with the error: "Content has been consumed". I understand that the content can only be consumed once but I get this error on the very first attempt and I don't see anywhere in the code where I'm possibly consuming it twice.

    private static String getData(String url, HttpParams params) {
    StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        if (params != null) {
            httpGet.setParams(params);
        }
        String result = "";
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
                content.close();
                result = builder.toString();
            } 
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    return result;  
    }
madhead
  • 31,729
  • 16
  • 153
  • 201
JBehrendt
  • 117
  • 1
  • 2
  • 10
  • are you sure exception is being generated into only this block of code – jeet Apr 16 '12 at 04:15
  • What is the result of calling `isStreaming` on your response entity? – Perception Apr 16 '12 at 04:19
  • Put a few logs in the various blocks and check if something is being called twice. If that doesn't help, provide the code where you are calling it and also the logcat. – Shubhayu Apr 16 '12 at 05:38

5 Answers5

6

make sure that you don't have in the Eclipse watch view something like http_response.getEntity() If you do, then this is what consumes your stream...

uris
  • 5,993
  • 3
  • 25
  • 24
2

Is this in the emulator or on your phone? It could be an emulator specific problem. I've test it on my device, and it works perfectly.

Do you perhaps have a debugger watch that could be consuming the content?

Zayin Krige
  • 3,229
  • 1
  • 35
  • 34
2

This can happen if you're consuming the entity more than once, in a similar call to this:

EntityUtils.toString(entity, HTTP.UTF_8))
dwbrito
  • 5,194
  • 5
  • 32
  • 48
1

Your get Data() method is perfect and it's working fine i already used this code to check and it's working perfectly for me.

so might there is a possibility that you called this method twice. if you want to check what i m using check below code i get result perfectly.

package com.sandeeppatel.httpget;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;

public class HttpGetActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://www.vogella.com");
    /*if (params != null) {
        httpGet.setParams(params);
    }*/
    String result = "";
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            content.close();
            result = builder.toString();
        } 
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
 // return result;  
}
}

And just i m giving the Internet permission only. Still if you not getting this give me your url and params.

Sandy
  • 985
  • 5
  • 13
  • That's what I thought. The code runs 100% fine on a colleagues pc but with me it gives "content has been consumed" on both my emulator and on my phone in debug mode. I upgraded the ADT and SDK just to be sure but that didn't solve the problem. – JBehrendt Apr 17 '12 at 07:00
1

I think your code is right. but try this to access string from HttpEntity: String response_str =EntityUtils.toString(responseEntity, HTTP.UTF_8);

like i used in my method:

    public String SetObjectSecurity(String username, String password,
        String clientName,String docRid,String ObjectRidsForCheckSum) throws JSONException, ClientProtocolException, IOException
{
    String SetObjectSecurityURL = "url";
    StringEntity str_request_entity = null;
    HttpResponse http_response = null;

    HttpGet getrequest = new HttpGet(SetObjectSecurityURL);
    postrequest.setHeader("Accept", "application/json");
    postrequest.setHeader("Content-type", "application/json");
//set param here


    HttpClient httpClient = new DefaultHttpClient();

    http_response = httpClient.execute(getrequest);

    //Log.e("Status code ",http_response);
    HttpEntity responseEntity = http_response.getEntity();

    String response_str =EntityUtils.toString(responseEntity, HTTP.UTF_8);
    Log.e("output",response_str);
    int i = http_response.getStatusLine().getStatusCode();
    Log.e("status","code "+i);


if(i==this){

do this}
else
{
this
}       
        return response_str;
        }
Rajiv yadav
  • 823
  • 8
  • 24