来自HttpURLConnection OkHttp3的连接泄漏警告

时间:2018-10-12 09:35:24

标签: java android okhttp3

我已经为这个问题苦苦挣扎了很长时间,而且我似乎在网上找不到任何答案。

在我的脑海中,我正在关闭所有应该关闭的东西。我从来没有得到响应正文,所以我不知道它可能是如何泄漏的,或者我应该如何关闭它。

我的代码:

    try {
        String urlString = AppConfig.probeNodeReportHost();
        URL url = new URL(urlString);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        DataOutputStream os = new DataOutputStream(conn.getOutputStream());
        os.writeBytes(json.toString());
        os.flush();
        os.close();

        conn.disconnect();
        return true;
    }catch(java.net.SocketTimeoutException e) {
        System.out.println("ERROR: Request timed out");
    }catch(java.net.UnknownHostException e){
        System.out.println("ERROR: Unknown host");
        if(AppConfig.hasNoInternetConnection()){
            System.out.println("NO INTERNET CONNECTION");
        }else{
            System.out.println("Has internet connection");
        }
    }catch (Exception e){
        e.printStackTrace();
    }finally{
        if (conn != null) {
            conn.disconnect();
        }
    }

编辑:我得到的确切错误消息是:

W/OkHttpClient: A connection to 'https://admin.ststage.se/' was leaked. Did you forget to close a response body?

0 个答案:

没有答案