本文介绍了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(),我不能让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();
    }
}

这篇关于AndroidHttpClient不能getEntity()的getContent()后关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:05