Search code examples
javaandroidandroid-asynctaskhttp-postapache-httpclient-4.x

Android - How to Handle an Async Http Crash


My app is currently crashing whenever it cannot connect to the server. How do I handle this, and instead let the user know that the server is down and to try again.

private void sendPostRequest(String givenEmail, String givenPassword) {

    class SendPostRequestTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            String emailInput = params[0];
            String passwordInput = params[1];

            String jsonUserInput = "{email: " + emailInput + ", password: "
                    + passwordInput + "}";

            try {

                HttpClient httpClient = new DefaultHttpClient();

                // Use only the web page URL as the parameter of the
                // HttpPost argument, since it's a post method.
                HttpPost httpPost = new HttpPost(SERVER_URL);

                // We add the content that we want to pass with the POST
                // request to as name-value pairs

                json = new JSONObject(jsonUserInput);

                jsonString = new StringEntity(json.toString());

                httpPost.setEntity(jsonString);

                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-type", "application/json");
                
                HttpParams httpParameters = httpPost.getParams();
                // Set the timeout in milliseconds until a connection is established.
                int timeoutConnection = 1000;
                HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                // Set the default socket timeout (SO_TIMEOUT) 
                // in milliseconds which is the timeout for waiting for data.
                int timeoutSocket = 1000;
                HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

                // HttpResponse is an interface just like HttpPost.
                // Therefore we can't initialize them
                HttpResponse httpResponse = httpClient.execute(httpPost);

                // According to the JAVA API, InputStream constructor does
                // nothing.
                // So we can't initialize InputStream although it is not an
                // interface
                InputStream inputStream = httpResponse.getEntity()
                        .getContent();

                InputStreamReader inputStreamReader = new InputStreamReader(
                        inputStream);

                BufferedReader bufferedReader = new BufferedReader(
                        inputStreamReader);

                StringBuilder stringBuilder = new StringBuilder();

                String bufferedStrChunk = null;

                while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
                    stringBuilder.append(bufferedStrChunk);
                }

                return stringBuilder.toString();

            } catch (ClientProtocolException cpe) {
                Log.i(LOGIN, "ClientProtocolException");
                cpe.printStackTrace();
            } catch (ConnectTimeoutException e) {
                e.printStackTrace();
            } catch (IOException ioe) {
                Log.i(LOGIN, "IOException");
                ioe.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            Log.i(LOGIN, result);

            try {
                serverResponse = new JSONObject(result);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                if ((serverResponse.has("status"))
                        && (serverResponse.get("status").toString()
                                .equals("200"))) {
                    Toast.makeText(getApplicationContext(), "SUCCESS!",
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Incorrect Email/Password!!!",
                            Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    SendPostRequestTask sendPostRequestTask = new SendPostRequestTask();
    sendPostRequestTask.execute(givenEmail, givenPassword);
}

(timeout code based on this answer by kuester2000)

LogCat Error Log

11-11 16:26:14.970: I/R.id.login_button(17379): IOException
11-11 16:26:14.970: W/System.err(17379): org.apache.http.conn.HttpHostConnectException: Connection to http://***** refused
11-11 16:26:14.980: W/System.err(17379):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)

Solution

  • I can see that you are already catching the Exceptions and have a String as parameter type to onPostExecute. From inside the exceptions, you can pass a string like "error" to the onPostExecute, whenever an error occurs. Inside the onPostExecute you can check:

    if the string is equal to "error":
        then create a Alert dialog box from within `onPostExecute` and show it. 
    else:
        continue as desired
    

    Ideally a boolean would do the trick but since you already have a string, you can also use that. Otherwise you can have a struct with a string and a boolean and then pass it to onPostExecute. Hope it gives you the idea.