gpt4 book ai didi

java - HttpConnection 不要关闭

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:25:08 25 4
gpt4 key购买 nike

我想关闭 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com