AndroidHttpClient不能getEntity()的getContent()后关闭AndroidHttpClient、getEntity、getContent

由网友(你微笑叹着气ㄣ笑我太致命)分享简介:public InputStream getInputStream() {AndroidHttpClient client = AndroidHttpClient.newInstance(USERAGENT);HttpUriRequest request = new HttpGet(url);InputStream i...
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(),我不能让InputSteam因为AndroidHttpClient关闭。 如果我不关闭AndroidHttpClient,出现泄密发现,AndroidHttpClient创造永不关闭。 如何让内容在这种情况下

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

推荐答案

与此类似,例如:

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();
    }
}
阅读全文

相关推荐

最新文章