我想在我的Android应用上使用Post方法获取内容,但总是在response.getEntity()
时崩溃,引发错误“已消耗内容”。我想知道出了什么问题。如果我尝试多次获取内容,则会出现该错误,但在我的代码中,我只尝试一次。
我的HttpPost
代码:
公共类MyActivity扩展了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;
}
}
}
感谢。
答案 0 :(得分:0)
这样做::
HttpEntity resEntity = responsePOST.getEntity();
if (statusCode == 200) {
String responseHistory = EntityUtils.toString(resEntity);
Log.i(getClass().getSimpleName(), responseHistory);
}