AndroidHttpClient关闭后无法getEntity().getContent()
public InputStream getInputStream() {
AndroidHttpClient client = AndroidHttpClient.newInstance(USERAGENT);
HttpUriRequest request = new HttpGet(url);
InputStream in = null;
try {
HttpResponse response = client.execute(request);
in = response.getEntity().getContent();
return in;
} catch (IOException e) {
e.printStackTrace();
} finally {
client.close();
}
}
我将此方法放在 Util 类中。 但是当在另一个类中调用 getInputStream() 时,由于 AndroidHttpClient 已关闭,我无法获取 InputSteam。 如果我不关闭AndroidHttpClient,则会出现“发现泄漏,AndroidHttpClient 创建且从未关闭”。 这种情况下如何获取内容
public InputStream getInputStream() {
AndroidHttpClient client = AndroidHttpClient.newInstance(USERAGENT);
HttpUriRequest request = new HttpGet(url);
InputStream in = null;
try {
HttpResponse response = client.execute(request);
in = response.getEntity().getContent();
return in;
} catch (IOException e) {
e.printStackTrace();
} finally {
client.close();
}
}
I put the this method in a Util class.
But when call getInputStream() in another class,I can't get the InputSteam because of AndroidHttpClient is closed.
if I don't close AndroidHttpClient, there appeared "leak found,AndroidHttpClient created and never closed".
How to get the content in this situation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样,例如:
Like this, for instance:
检查您的清单文件是否启用了 INTERNET 权限。
将以上行包含在您的 Androidmanifest.xml 中
Check whether you have enabled the INTERNET Permission on your manifest file.
Include the above line in your Androidmanifest.xml