首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >http调用执行中的资源泄漏

http调用执行中的资源泄漏
EN

Stack Overflow用户
提问于 2012-12-10 10:48:12
回答 1查看 1.2K关注 0票数 1

执行此代码时,我将收到以下错误消息。

一个资源是在附加的堆栈跟踪中获得的,但从未释放过。有关避免资源泄漏的信息,请参见java.io.Closeable .:

我无法识别以下代码中的资源泄漏。如果有人指出我到底做错了什么,我会很高兴的。

代码语言:javascript
复制
HttpPost request = new HttpPost(url);
        StringBuilder sb = new StringBuilder();
        StringEntity entity = new StringEntity(jsonString);
        entity.setContentType("application/json;charset=UTF-8");
        entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
        request.setHeader("Accept", "application/json");
        request.setEntity(entity);

        HttpResponse response = null;
        DefaultHttpClient httpclient = getHttpClientImpl();

        BufferedReader reader = null;
        InputStream in = null;
        try {
            response = httpclient.execute(request);
            in = response.getEntity().getContent();
            reader = new BufferedReader(new InputStreamReader(in));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception se) {
            Log.e("Exception", se + "");
            throw se;
        } finally {
            if (in != null)
                in.close();

            if (reader != null)
                reader.close();

            if (client != null && client.getConnectionManager() != null) {
        client.getConnectionManager().shutdown();
    }
        }
        return sb.toString();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-10 11:00:37

我并不认为该代码存在任何问题,但我建议关闭池中的所有空闲连接。

代码语言:javascript
复制
public abstract void closeIdleConnections (long idletime, TimeUnit tunit)

此外,当DefaultHttpClient没有完全正确配置时,也存在一些已知的问题。我建议使用AndroidHttpClient作为HttpClient接口的实现。请访问下列文件以获得解释:

http://developer.android.com/reference/android/net/http/AndroidHttpClient.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13799411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档