Android IllegalStateException:内容已被使用(在第一个getContent()上)
线路Android IllegalStateException:内容已被使用(在第一个getContent()上),android,android-asynctask,http-get,Android,Android Asynctask,Http Get,线路 private HttpResponse doResponse(String url) { HttpClient httpclient = new DefaultHttpClient(getHttpParams()); HttpResponse response = null; try { HttpGet httpget = new HttpGet(url); response = httpclient.execute(httpge
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
我在三星galaxy tab 2上运行Android 4.1.1确保只调用一次
getContent()。验证在inputStreamToString()
方法中没有调用它getContent()
返回输入流,每个连接只能返回一次输入流
:
getContent()
创建实体的新InputStream对象多次返回同一InputStream对象是编程错误。如果多次调用此方法,则不可重复的实体将引发异常
请向我们展示inputStreamToString()
@Asok thx的代码以获取帮助(我无法解释原因),但这里的异常仅限于调试模式,运行模式没有问题,可能是http请求超时。请确保只调用getContent()
,请确保您没有在方法inputStreamToString()
中调用它。我在Xamarin.Android中也只有在调试时才会出现此错误。我想,调试器应该在我们面前阅读内容。
response.getEntity().getContent();