执行此代码时,我收到以下错误消息。
在附加的堆栈跟踪中获取资源但从未发布。有关避免资源泄漏的信息,请参阅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();
答案 0 :(得分:1)
我真的没有看到该代码有任何问题,但我建议关闭池中的所有空闲连接。
public abstract void closeIdleConnections (long idletime, TimeUnit tunit)
此外,如果没有完全正确配置,DefaultHttpClient存在一些已知问题。我建议使用AndroidHttpClient作为HttpClient接口的实现。请访问以下文档以获取解释:
http://developer.android.com/reference/android/net/http/AndroidHttpClient.html