0

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());
    }
Community
  • 1
  • 1
jerrytouille
  • 1,238
  • 1
  • 12
  • 28
  • is your webserver is running ? – Riskhan Jan 17 '13 at 06:03
  • no i disabled it to simulate server down to test the exception. – jerrytouille Jan 17 '13 at 06:08
  • Can you post the full LogCat? – Juan Gomez Jan 17 '13 at 06:37
  • @juandg http://pastebin.com/rpe8iKRi also added to original post. – jerrytouille Jan 17 '13 at 07:35
  • What is at this line? com.project.look.LoginActivity$LoginTask.doInBackground(LoginActivity.java:95) – Alin Jan 17 '13 at 10:16
  • @Alin that line (95) check the json response params from the server that this line (94) requested: _JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);_ which _getJSONFromUrl()_ is the function in original post, which supposed to throw the ConnectionTimeOut exception and catched. – jerrytouille Jan 17 '13 at 11:37
  • You have a NullPointerException at that line (check the LogCat), make sure that variable gets initialized. – Alin Jan 17 '13 at 14:14
  • @Alin of course _json_ on line 95 (_if (json.getString(KEY_SUCCESS) != null)_) is null because the _getJSONFromUrl()_ function failed on line 94 (_json = uF.loginUser(username, password);_) above it with ConnectionTimeOut exception and catched. Bottom line is: it should have stopped at line 94 and display toast "server timeout", not go on to line 95 to get that NullPointerException and so on. – jerrytouille Jan 17 '13 at 17:30
  • it doesn't stop because you don't re-throw the exception. Toast.makeText.. just displays a toast message – Alin Jan 17 '13 at 17:42
  • @Alin let's say I rethrow and recatch, what can I do beside displaying a toast message so that it wont crash the app? btw can you +1 vote to this question so it gets a bit more attention? thanks. – jerrytouille Jan 18 '13 at 01:28

1 Answers1

2

got it resolved. missing throws Exception { at the end of the function. duhhh

thanks @Alin for good hint.

jerrytouille
  • 1,238
  • 1
  • 12
  • 28