Java http连接don';关不上
我想关闭get请求连接,但它没有关闭 这是我的代码块 编辑代码块,但不再工作Java http连接don';关不上,java,android,http,httpclient,androidhttpclient,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
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();
}
要了解更多信息,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();
}
若要阅读有关此的详细信息,请参见HttpResponse类没有close方法sir。@Zarzamora没有使用HttpResponse,请尝试CloseableHttpResponse,而不是使用HttpResponse。HttpResponse内部org.apache.http包sirHttpResponse类没有close方法sir。@Zarzamora没有使用HttpResponse,try closeablehttpresponseere不是CloseableHttpResponse内部org.apache.http包sir