IllegalStateException:使用httpPost从服务器获取数据已消耗内容

时间:2014-04-24 21:32:37

标签: android exception

我想在我的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;
    }


}

}

感谢。

1 个答案:

答案 0 :(得分:0)

这样做::

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