2013-12-20 166 views
0

在我的活動中,我使用httpurl連接調用一個url。
在網絡強度非常低的情況下,我想要斷開該請求。Android HttpURLConnection斷開連接

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

我想在按下後退按鈕時取消請求。

回答

-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); 

TIMEOUT基本上是用於慢速連接。在那段時間之後,你的連接將斷開連接

+0

這個答案與按下按鈕後取消請求無關,它根本不涉及'HttpURLConnection'。 –

相關問題