private HttpResponse doResponse(String url) {
    HttpClient httpclient = new DefaultHttpClient(getHttpParams());
    HttpResponse response = null;

    try {
        HttpGet httpget = new HttpGet(url);
        response = httpclient.execute(httpget);
    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
    }

    return response;
}

@Override
protected String doInBackground(String... urls) {
    // TODO: attempt authentication against a network service.
    String url = urls[0];
    String result ="";

    try {
        HttpResponse response=doResponse(url);
        result = inputStreamToString(response.getEntity().getContent());
        //throws IllegalStateException: Content has been consumed
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
    }

    return result;
}


线

result = inputStreamToString(response.getEntity().getContent());


抛出

IllegalStateException: Content has been consumed


即使我以前没有得到内容。

我想知道我的代码中的女巫部分在使用内容之前

response.getEntity().getContent();


我在Samsung Galaxy Tab 2上
运行Android 4.1.1

最佳答案

确保只调用一次getContent()。确认您没有在inputStreamToString()方法中调用它。 getContent()返回一个输入流,每个连接只能返回一次输入流。

Documentation


  getContent()
  
  创建实体的新InputStream对象。多次返回同一个InputStream对象是编程错误。如果多次调用此方法,则不可重复的实体将引发异常。

10-04 23:09