0

我正在使用下面的代碼來檢查Android中的連接超時時間。最重要的是,如果連接實際超時,我該如何顯示吐司?任何建議?Android如何在連接上顯示吐司時間

/*Client timeout*/ 
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;  //3Seconds 
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data. 
int timeoutSocket = 5000;   //5Seconds 
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
/*Client timeout ends*/ 

final HttpClient client = new DefaultHttpClient(httpParameters); 

********************** 
//HOW TO SHOW TOAST MESSAGE WHEN THIS CASE ACTUALLY OCCURS ?? 

回答

1

您可以使用try catch塊。

try{ 
    //your code of making request 
} 
catch (ConnectTimeoutException e) { 
    Toast.makeText(context, "Connection timed out.", Toast.LENGTH_SHORT).show();  
} 

確保從活動中傳遞適當的上下文。

希望這會有所幫助。

+0

難道我沒有得到任何迴應?我的意思是像「504」代碼? – user45678 2014-10-07 08:05:55

+0

我想這將返回response = null。 :) @ user45678 – 2014-10-07 08:06:32

+0

你確定嗎?然後我可以直接檢查空響應? – user45678 2014-10-07 08:06:58