java - HttpConnection 不要关闭

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

我想在请求后关闭连接

最佳答案

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

关于java - HttpConnection 不要关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43668102/

相关文章:

java - 我无法加载带有 spring security 的登录后可以访问的 CSS 页面。我该如何解决?

java - 下载并阅读网页源代码,违法吗?

java - 几分钟后,Spring Boot 停止解析 View

android - 如何将 Cookie session id 放入 volley request 中?

node.js - 内存泄漏 Meteor.http

java - 在 JPA 中使用 GROUP BY

c# - 尝试将代码块从 C# 转换为 Java (Android)

java - 如何使文件加密?

Delphi TIdHTTP POST 与 GET 相比非常慢

c - C 中的 SSL/HTTPS 客户端