IllegalStateException异常:内容一直使用httpPost从服务器获取数据消耗 [英] IllegalStateException: Content has been consumed using httpPost to get data from server

查看:250
本文介绍了IllegalStateException异常:内容一直使用httpPost从服务器获取数据消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Post方法在我的Andr​​oid应用程序来获得内容,但总是在 response.getEntity崩溃()引发错误的内容已经被消费 的。我不知道什么是错的。如果我尝试获取内容多次出现该错误,但是在我的code,我只尝试一次。

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.

我的 HttpPost code:

公共类MyActivity延伸活动{

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;
    }


}

}

感谢。

推荐答案

做这样的方式:

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

这篇关于IllegalStateException异常:内容一直使用httpPost从服务器获取数据消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆