IllegalStateException: Content has been consumed using httpPost to get data from server

被刻印的时光 ゝ 提交于 2020-01-06 20:37:39

问题


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.


回答1:


Do it like this way::

 HttpEntity resEntity = responsePOST.getEntity();
 if (statusCode == 200) {
            String responseHistory = EntityUtils.toString(resEntity);
            Log.i(getClass().getSimpleName(), responseHistory);
        } 


来源:https://stackoverflow.com/questions/23280278/illegalstateexception-content-has-been-consumed-using-httppost-to-get-data-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!