如何解决W / OkHttpClient:连接泄漏?

时间:2018-12-30 12:17:28

标签: java https

我正在尝试建立到某个远程服务器的连接,并在LogCat中收到以下警告:"W/OkHttpClient: A connection to "some url" was leaked. Did you forget to close a response body?" 现在我不明白为什么会这样。 我使用Apache IO Commons来计算发送和接收的字节数,这是我唯一使用OkHTTP的方法。 你能看看我的代码片段,告诉我这是怎么回事吗?

这是我尝试创建连接的代码:

public static JSONObject getConfiguration(Context context) throws HttpRequestException {

        long bytes = 0;
        long netUsage = HttpUtils.getCurrentNetworkUsage(context);

        int statusCode = 0, count = 0;
        NetworkRequest.Status netStatus = NetworkRequest.Status.FAILED;

        JSONObject commonInformation;
        HttpURLConnection connection = null;
        CountingInputStream cis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        InputStream is = null;
        CountingOutputStream cos = null;
        // result
        JSONObject receivedConfig = null;

        try {

            commonInformation = ConfigurationProcessor.getCommonInformation(context);
            if (commonInformation == null) {
                return null;
            }

            URL url = new URL(BuildConfig.SERVER_CONFIG_URL);
            if (BuildConfig.DEBUG) {
                LogUtils.d(TAG, "url = " + url.getPath());
            }

            connection = url.getProtocol().equals("https")? getHttpsConnection(url) : getHttpConnection(url);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            connection.setRequestProperty("Content-Encoding", "gzip");

            byte[] gzipped = HttpUtils.gzip(commonInformation.toString());
            cos = new CountingOutputStream(connection.getOutputStream());
            cos.write(gzipped);
            cos.flush();

            statusCode = connection.getResponseCode();

            switch (statusCode) {

                case 200: {
                    // get the response
                    is = connection.getInputStream();
                    cis = new CountingInputStream(is);
                    isr = new InputStreamReader(cis);
                    br = new BufferedReader(isr);

                    StringBuilder builder = new StringBuilder();
                    String output;
                    while ((output = br.readLine()) != null) {
                        builder.append(output);
                    }

                    receivedConfig = new JSONObject(builder.toString());

                    netStatus = NetworkRequest.Status.SUCCEEDED;
                    break;
                }
                case 502: {
                    throw new HttpRequestException("Received 502 error (Bad Gateway)", NetworkRequest.Type.GET_REMOTE_CONFIGURATION);
                }
            }

            // analytics about the request
            if (BuildConfig.DEBUG) {
                LogUtils.d(TAG, "generating analytics about the http request");
            }
            count = receivedConfig == null ? 0 : 1;

            if (cis != null) {
                bytes = cis.getByteCount();
            }

            if (cos != null) {
                bytes += cos.getByteCount();
            }

        } catch (Exception | OutOfMemoryError ex) {
            LogUtils.e(TAG, ex.getMessage());
            ex.printStackTrace();
        } finally {

            closeStream(is);
            closeStream(br);
            closeStream(isr);
            closeStream(cis);
            closeStream(cos);
            if (connection != null) connection.disconnect();
            createNetworkRequest(context, statusCode, netUsage, netStatus, count, bytes, NetworkRequest.Type.GET_REMOTE_CONFIGURATION, BuildConfig.SERVER_CONFIG_URL);
        }

        if (BuildConfig.DEBUG) {
            LogUtils.d(TAG, "Received configuration data");
        }
        return receivedConfig;
    }

0 个答案:

没有答案