4

I'm using HttpURLConnection to retrieve some configuration from a server. It works fine but for some reason I'm getting the following warning in the logcat:

OkHttpClient: A connection to ... was leaked. Did you forget to close a response body?

As pointed out in this question, Android is using OkHttp internally in HttpUrlConnection. What am I doing to cause this warning?

Here is the code I'm using:

    HttpURLConnection connection = null;
    OutputStream outputStream = null;
    String result;
    try {
        URL url = new URL(CONFIG_URL);
        connection = (HttpURLConnection) url.openConnection();
        connection.setReadTimeout(READ_TIMEOUT_MILLI);
        connection.setConnectTimeout(CONNECT_TIMEOUT_MILLI);
        connection.setRequestMethod(REQUEST_METHOD);

        connection.setDoInput(true);

        connection.addRequestProperty("Content-Type", "application/json");
        connection.addRequestProperty("Accept", "application/json");

        outputStream = connection.getOutputStream();
        try (DataOutputStream wr = new DataOutputStream(outputStream)) {
            wr.write(data.toString().getBytes(STRING_ENCODING));
            wr.flush();
            wr.close();

            int responseCode = connection.getResponseCode();

            if (responseCode != HttpsURLConnection.HTTP_OK) {
                Log.e(TAG, "HTTP error code: " + responseCode);
                return;
            }

            try (InputStream stream = connection.getInputStream()) {
                if (stream != null) {
                    result = readStream(stream, READ_STREAM_MAX_LENGTH_CHARS);
                    //...
                    connection.disconnect();
                }
            } catch (Throwable e) {
                Log.e(TAG, "run: failed to parse server response: " +e.getMessage());
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "HttpSendTask: failed to send configuration request " + data +": " +e.getMessage());
    } finally {
        if (outputStream != null) {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
Sir Codesalot
  • 7,045
  • 2
  • 50
  • 56
  • Definitely the right app and there are no dependencies (HttpURLConnection is part of java.io) – Sir Codesalot Jun 13 '18 at 12:07
  • @tyczj not a duplicate. edited the question. – Sir Codesalot Jun 14 '18 at 04:47
  • how did you solve this problem? – Keselme Dec 30 '18 at 15:34
  • @Keselme unfortunately I didn't. The most helpful thing I came across is using a regular expression to filter these lines. – Sir Codesalot Dec 31 '18 at 07:47
  • actually I found a solution that worked for me, after reading this blog: https://blog.csdn.net/Gaugamela/article/details/78482564 (I had to translate since it's in some Asian langauge), I figured out that the leaked connection was the inputstream, so when I closed the input string, that warning went away. From your code I see that you only closed the outputstream. – Keselme Dec 31 '18 at 08:31
  • @Keselme but the inputstream is opened in the try with resources block which means it's supposed to be closed automatically at the end of the block. – Sir Codesalot Jan 02 '19 at 15:08
  • Late, but if it can help, check this answer: https://stackoverflow.com/questions/47666053/okhttp-connection-leak-log-line-even-when-okhttp-is-not-a-dependency/62059933#62059933 – Lisitso May 28 '20 at 11:09

1 Answers1

0

Closing the inputstream of the connection solved my problem. Try to close it in the finally block :

connection.getInputStream().close()