Basically I'm trying to let user know when the app cannot reach the server/server down, while their phone internet/data connection is perfectly fine. So I set a timeout by following this post here: How to set HttpResponse timeout for Android in Java
It works great, except when the exception is caught correctly the app still crash instead of displaying the Toast message and return to the app screen. Notice that "Log.e("CONN TIMEOUT", e.toString());" logged correctly in logcat as: "CONN TIMEOUT org.apache.http.conn.ConnectTimeoutException: Connect to /192.168.11.60:80 timed out"
As requested, full logcat: http://pastebin.com/rpe8iKRi
// Making HTTP request
try {
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
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ConnectTimeoutException e) {
Toast.makeText(getApplicationContext(), "Server timeout", Toast.LENGTH_LONG).show();
Log.e("CONN TIMEOUT", e.toString());
} catch (SocketTimeoutException e) {
Toast.makeText(getApplicationContext(), "Server timeout", Toast.LENGTH_LONG).show();
Log.e("SOCK TIMEOUT", e.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
Log.e("OTHER EXCEPTIONS", e.toString());
}