Android - HttpUrlConnection is not closing. Eventually results to SocketException

前端 未结 4 1212
我在风中等你
我在风中等你 2020-12-15 11:03

I am encountering some problems with the HttpUrlConnection in devices running Jellybean (4.1 - 4.3) wherein connections are not closed and results to a SocketException \"Too

4条回答
  •  既然无缘
    2020-12-15 11:42

    Check If you have tried all of the below... There might be something missing.. other wise it should not have any problem.

    InputStream in;
    HttpsURLConnection urlConnection =null;
    try {
        URL url = new URL(Url);
    
        urlConnection = (HttpsURLConnection) url
                         .openConnection();
        //5 Second timeout
        urlConnection.setReadTimeout(5*1000);
    
        in = urlConnection.getInputStream();
        int responseCode = urlConnection.getResponseCode();
    
        if (responseCode != HttpURLConnection.HTTP_OK) {
             InputStream errInputStream = urlConnection.getErrorStream();
            //Print error message and response code..
             errInputStream.close();
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally{
        if(urlConnection != null)
            urlConnection.disconnect();
    }
    

提交回复
热议问题