gpt4 book ai didi

AndroidHttpClient 关闭后无法 getEntity().getContent()

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:27 26 4
gpt4 key购买 nike

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);
return new ByteArrayInputStream(new EntityUtils.toByteArray(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
} finally {
client.close();
}
}

关于AndroidHttpClient 关闭后无法 getEntity().getContent(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9393426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com