Strict Mode complains on resource leak

android, android-strictmode, bytearrayoutputstream, inputstream, java

Solution

As Praful Bhatanagar pointed out, you need to release resources in finally block:

HttpClient httpclient = new DefaultHttpClient();
//... code skipped
String responseString = "";
try {
//... code skipped
} catch (IOException e) {
} finally {
     httpClient.getConnectionManager().shutdown();
}

Problem

Strict mode complains the following: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.: ``` **response = httpclient.execute(httpPost);** ``` Below is my code: ``` HttpClient httpclient = new DefaultHttpClient(); String url = "example"; HttpPost httpPost = new HttpPost(url); HttpResponse response; String responseString = ""; try { httpPost.setHeader("Content-Type", "application/json"); **response = httpclient.execute(httpPost);** StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); } else { response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (ClientProtocolException e) { } catch (IOException e) { } return responseString; ``` Thanks in advance.

Original source

Related problems