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;
}
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?
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...
This can happen if you're consuming the entity more than once, in a similar call to this:
EntityUtils.toString(entity, HTTP.UTF_8))
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.
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;
}