Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HttpResponse - Content has been consumed

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;  
    }
like image 502
JBehrendt Avatar asked Apr 16 '12 04:04

JBehrendt


1 Answers

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

like image 103
uris Avatar answered Sep 19 '22 05:09

uris