HttpConnection不关闭

时间:2017-04-27 21:20:35

标签: java android http httpclient androidhttpclient

我想关闭get请求连接但是没有关闭。

我的代码块

编辑代码块但不能再次使用..

 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();
        }

我想在请求后关闭连接

1 个答案:

答案 0 :(得分:0)

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