2012-12-10 29 views
1

我在執行此代碼時收到以下錯誤消息。http調用執行中的資源泄漏

資源在附加的堆棧跟蹤中獲取,但從未發佈。有關避免資源泄漏的信息,請參閱java.io.Closeable .:

我無法確定下面代碼中的資源泄漏。如果有人指出我究竟做錯了什麼,我會很感激。

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(); 

回答

1

我真的沒有看到該代碼的任何問題,但我建議關閉池中的所有空閒連接。

public abstract void closeIdleConnections (long idletime, TimeUnit tunit) 

此外,也有一些已知的問題與DefaultHttpClient時並不完全正確配置。我建議使用AndroidHttpClient作爲HttpClient接口的實現。請訪問以下文檔以獲得解釋:

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