首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpConnection不关闭

HttpConnection不关闭
EN

Stack Overflow用户
提问于 2017-04-28 05:20:35
回答 1查看 45关注 0票数 1

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

这是我的代码块

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

代码语言:javascript
复制
 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();
        }

我想在请求后关闭连接

EN

回答 1

Stack Overflow用户

发布于 2017-04-28 05:27:24

Android的方式与java的方式稍有不同。你能试试这种方法吗?

代码语言:javascript
复制
   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的更多信息

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43668102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档