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();
我在三星galaxy tab 2上 运行Android 4.1.1
答案 0 :(得分:1)
确保您只拨打getContent()
一次。验证您的inputStreamToString()
方法中没有调用它。 getContent()
返回一个输入流,每个连接只能返回一个输入流。
getContent()
创建实体的新InputStream对象。 多次返回相同的InputStream对象是一个编程错误 。如果多次调用此方法,则不可重复的实体将抛出异常。