Android HttpURLConnection断开连接

时间:2013-12-20 04:37:30

标签: android httpurlconnection

在我的活动中,我使用httpurl连接呼叫网址  在网络强度非常低的情况下,我想断开该请求。

  How can i disconnect an httpurl connection request that is already been sent?

我想在按下后退按钮时取消请求。

1 个答案:

答案 0 :(得分:-1)

HttpGet httpGet = new HttpGet(url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used. 
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);

超时基本上是为了缓慢连接。在很长一段时间之后,您的连接将断开连接