【问题标题】:HttpConnection don't closeHttpConnection 不关闭
【发布时间】:2017-09-25 20:35:28
【问题描述】:

我想关闭获取请求连接,但是没有关闭。

这是我的代码块

编辑代码块但不再工作..

 try {
            HttpClient client = new DefaultHttpClient();
            URI website = website = new URI("http://"+IPAdres+"/?Arduino="+Komut);
            HttpGet request = new HttpGet();
            request.setURI(website);

            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            EntityUtils.consume(entity);
            request.abort();
            client.getConnectionManager().shutdown();
            client.getConnectionManager().closeExpiredConnections();

        } catch (URISyntaxException e) {
            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        } catch (ClientProtocolException e) {
            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }

我想在请求后关闭连接

【问题讨论】:

    标签: java android http httpclient androidhttpclient


    【解决方案1】:

    Android 方式与 java 方式略有不同。你可以试试这个方法吗?

       URL url = new URL("http://www.android.com/");
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
       try {
         InputStream in = new BufferedInputStream(urlConnection.getInputStream());
         readStream(in);
       } finally {
         urlConnection.disconnect();
       }
    

    阅读更多关于try this link

    【讨论】:

    • HttpResponse 类没有关闭方法先生。
    • @Zarzamora 不要使用 HttpResponse,试试 CloseableHttpResponse
    • 没有CloseableHttpResponse内部org.apache.http包先生
    猜你喜欢
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2012-01-27
    • 2014-10-21
    • 2011-09-04
    • 2018-12-12
    • 1970-01-01
    相关资源
    最近更新 更多