Android HttpResponse - content has been consumed

The method below crashes when reading the HttpResponse with the error: "Content was destroyed." I understand that content can only be used once, but I get this error on the first try, and I don’t see anywhere in the code where I possibly use 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; } 
+6
source share
5 answers

Is it in the emulator or on the phone? This may be a problem with the emulator. I tested it on my device and it works great.

Perhaps you have a debugger that can consume content?

+2
source

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

+6
source

This can happen if you consume an object more than once, in the same order:

 EntityUtils.toString(entity, HTTP.UTF_8)) 
+2
source

Your get () method works fine and works fine, I already used this code for verification, and it works fine for me.

it is possible that you called this method twice. if you want to check what I use to check below code, I get the 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 only I provide only permission to the Internet. However, if you do not get this, give me your url and params.

+1
source

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

as 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; } 
+1
source

Source: https://habr.com/ru/post/913290/


All Articles