4

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.

muneikh
  • 1,959
  • 4
  • 23
  • 54

2 Answers2

7

As of 4.3 the method pointed out by kenota is deprecated.

Instead of HttpClient you should now use CloseableHttpClient as shown below:

    CloseableHttpClient client= HttpClientBuilder.create().build();

Then you can close it using:

    client.close();
Tspoon
  • 3,450
  • 1
  • 23
  • 33
  • [Can't find it](http://developer.android.com/reference/packages.html#q=CloseableHttpClient) in the documentation, do you have a link ? – Alexander Malakhov Jan 14 '14 at 12:08
  • 2
    [Apache Docs](http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/CloseableHttpClient.html) - Apparantly Android doesn't actually use a specific release version of Apache HTTP ([source](http://stackoverflow.com/questions/2618573/what-version-of-apache-http-client-is-bundled-in-android-1-6)). Which I guess means it's okay to use the deprecated method mentioned by kenota (although I haven't actually tested this). – Tspoon Jan 14 '14 at 12:56
  • Why doesn't `HttpClient` interface have a `close` method when it should be closeable? – ADTC Jul 14 '15 at 06:50
  • 1
    @ADTC: From the docs: `This interface represents only the most basic contract for HTTP request execution. It imposes no restrictions or particular details on the request execution process and leaves the specifics of state management, authentication and redirect handling up to individual implementations.` I guess they just decided it didn't belong there - can't say why though. – Tspoon Jul 14 '15 at 13:46
  • 2
    The funny thing is the compiler warns about resource leak, and so when we want to fix it, there is no `close()` method. Then they had to come up with the `Closeable` version to solve this problem. **Messy!** – ADTC Jul 15 '15 at 01:24
4

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();
}
kenota
  • 5,510
  • 1
  • 13
  • 11