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