I want to get content using Post method on my Android App, but always crash on response.getEntity()
throwing the error "Content has been consumed". I wonder what is wrong. That error appears if I try to get content several times, but on my code I only try once.
My HttpPost
code:
public class MyActivity extends Activity {
/*****************************************/
/*** FUNCION POST - RECUPERAMOS DATOS ***/
/*****************************************/
public class TaskGet extends AsyncTask<String, Integer, String> {
TaskGet obj = this;
BufferedReader in = null;
String stringifiedResponse;
JSONObject data = null;
public TaskGet( ) {
super();
}
@Override
protected String doInBackground(String... arg0) {
Log.i(getClass().getSimpleName(), "GET task - start");
try {
String url = "http://XXXXXX.com/xxxxxx";
HttpPost httpRequestHistory = new HttpPost(url);
String auth = "xxxxxxxx";
httpRequestHistory.addHeader("Authorization", "Basic " + auth);
HttpClient httpclient = new DefaultHttpClient();
// LOG
Log.i(getClass().getSimpleName(), "called GET HISTORY");
// PARAMETROS
String oInt = "10";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("startDate","1393445183"));
nameValuePairs.add(new BasicNameValuePair("endDate","1393445198"));
nameValuePairs.add(new BasicNameValuePair("filterNumber", oInt));
httpRequestHistory.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpRequestHistory);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
String responseHistory = EntityUtils.toString(response.getEntity());
Log.i(getClass().getSimpleName(), responseHistory);
}
Log.i(getClass().getSimpleName(), "GET task - end");
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
protected void onPostExecute(String result) {
stringifiedResponse = result;
}
}
}
Thanks.