0

I am using below code (from this answer by kuester2000) to check the connection time out in Android. On top of this how can I show Toast if connection actually times out? Any suggestion?

/*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 ??
Ryan M
  • 18,333
  • 31
  • 67
  • 74
user45678
  • 1,504
  • 6
  • 29
  • 58

1 Answers1

1

You can use try catch block for that.

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

Make sure to pass proper context from Activity.

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124