【问题标题】:httpclient leak found after a day work一天工作后发现httpclient泄漏
【发布时间】:2012-12-10 12:12:48
【问题描述】:

如果在我的应用程序中使用此源代码,第一次没有问题,但是如果我关闭 wifi 并尝试从 Web 服务获取一些资源,我的应用程序会抛出异常状态:发现泄漏,我使用关闭httpclient 的方法,但没有任何变化,在使用此代码的两天后,会引发异常。

堆栈打印:

10-17 16:42:00.524: ERROR/AndroidHttpClient(931): Leak found
10-17 16:42:00.524: ERROR/AndroidHttpClient(931): java.lang.IllegalStateException: AndroidHttpClient created and never closed

代码:

public String getRequest(String url, ArrayList<NameValuePair> nameValuePairs){
    String resp = "";

        HttpParams httpParameters = new BasicHttpParams();
        HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
        HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);

        AndroidHttpClient httpclient = null;

        try {

            httpclient  = AndroidHttpClient.newInstance("V");

            httpclient.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);


            HttpPost httppost = new HttpPost(url);

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));  

            HttpResponse response = httpclient.execute(httppost);

            HttpEntity entity = response.getEntity();
            InputStream instream = entity.getContent();

            Reader reader = new InputStreamReader(instream, HTTP.UTF_8);

            StringBuilder buffer = new StringBuilder("");

            char[] tmp = new char[1024];
            int l;
            while ((l = reader.read(tmp)) != -1) {
                buffer.append(tmp, 0, l);
            }

            reader.close();

            resp = buffer.toString().replace("&quot;", "\"");

            Log.d("V", "RESPONSE:-----\n" + resp + "\n--------------");


        } catch (IOException e){

            Log.e("V IOException [Send Request]","IO Exception");
            if((e != null) && (e.getMessage() != null)){
                Log.e("V",e.getMessage().toString());
            }

        } catch (Exception e) { 

            Log.e("V Exception [Send Request]","Exception Requester");
            if((e != null) && (e.getMessage() != null)){
                Log.e("V",e.getMessage().toString());
            }

        }finally {
            if (httpclient != null && httpclient.getConnectionManager() != null) {
                httpclient.close();
                httpclient = null;
            }
        }

        return resp;

    }

【问题讨论】:

    标签: java android memory-leaks urlconnection


    【解决方案1】:

    试试:httppost.abort();在最后。另外,我不认为为每个请求创建一个 httpclient 是一个好主意,我认为最好将 http 客户端创建为实例变量并为每个请求重用。

    【讨论】:

    • 如果只使用一个httpclient,连接冻结,一分钟后,查询超时。这发生在应用程序运行大约两个小时后。我改变了连接-> wifi-3G 也会发生同样的情况
    • 如果我理解得很好,你有一个方法“getRequest”,所以你的所有请求都会调用这个方法。每次您调用“getRequest”时,您都在创建一个新的 httpclient,您也尝试过我所说的吗?添加if (httppost != null) { httppost.abort(); }
    【解决方案2】:

    我可能会建议使用 EntityUtils 类来做你正在做的事情,而不是你正在尝试做的事情。它会解决大部分问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      相关资源
      最近更新 更多