How to caught exception for org.apache.http.client.HttpClient when the target machine is not reachable?
<p>I am running a http connection (using HttpClient.execute) in a asyntask.doInBackgroud. When the http server is not up, I expect my android app prompt the user with the proper information something like "please try again later." But however I try to caught the exception, my app will crash directly. There is no chance to prompt the user about the situation.</p>

<p>I attached the log below, </p>

<pre><code>07-13 03:50:51.605: E/Util.java, function sendPost(1137): IOException encountered.
07-13 03:50:51.605: W/System.err(1137): org.apache.http.NoHttpResponseException: The target server failed to respond
07-13 03:50:51.605: W/System.err(1137):     at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
07-13 03:50:51.605: W/System.err(1137):     at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
07-13 03:50:51.605: W/System.err(1137):     at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
07-13 03:50:51.605: W/System.err(1137):     at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
07-13 03:50:51.605: W/System.err(1137):     at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
07-13 03:50:51.605: W/System.err(1137):     at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
07-13 03:50:51.615: W/System.err(1137):     at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
07-13 03:50:51.615: W/System.err(1137):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
07-13 03:50:51.624: W/System.err(1137):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-13 03:50:51.624: W/System.err(1137):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-13 03:50:51.624: W/System.err(1137):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-13 03:50:51.624: W/System.err(1137):     at game.location.Util.sendPost(Util.java:27)
07-13 03:50:51.624: W/System.err(1137):     at game.location.LoginForm$LoginTask.doInBackground(LoginForm.java:73)
07-13 03:50:51.624: W/System.err(1137):     at game.location.LoginForm$LoginTask.doInBackground(LoginForm.java:1)
07-13 03:50:51.634: W/System.err(1137):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-13 03:50:51.634: W/System.err(1137):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-13 03:50:51.634: W/System.err(1137):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-13 03:50:51.664: W/System.err(1137):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-13 03:50:51.664: W/System.err(1137):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-13 03:50:51.664: W/System.err(1137):     at java.lang.Thread.run(Thread.java:1096)
07-13 03:50:51.664: W/dalvikvm(1137): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
07-13 03:50:51.694: E/AndroidRuntime(1137): FATAL EXCEPTION: AsyncTask #1
07-13 03:50:51.694: E/AndroidRuntime(1137): java.lang.RuntimeException: An error occured while executing doInBackground()
07-13 03:50:51.694: E/AndroidRuntime(1137):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at java.lang.Thread.run(Thread.java:1096)
07-13 03:50:51.694: E/AndroidRuntime(1137): Caused by: java.lang.NullPointerException
07-13 03:50:51.694: E/AndroidRuntime(1137):     at game.location.Util.sendPost(Util.java:37)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at game.location.LoginForm$LoginTask.doInBackground(LoginForm.java:73)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at game.location.LoginForm$LoginTask.doInBackground(LoginForm.java:1)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-13 03:50:51.694: E/AndroidRuntime(1137):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-13 03:50:51.694: E/AndroidRuntime(1137):     ... 4 more
07-13 03:50:51.943: W/IInputConnectionWrapper(1137): showStatusIcon on inactive InputConnection
07-13 03:50:53.114: E/WindowManager(1137): Activity game.location.LoginForm has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f5cbf0 that was originally added here
07-13 03:50:53.114: E/WindowManager(1137): android.view.WindowLeaked: Activity game.location.LoginForm has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f5cbf0 that was originally added here
07-13 03:50:53.114: E/WindowManager(1137):  at android.view.ViewRoot.&lt;init&gt;(ViewRoot.java:247)
07-13 03:50:53.114: E/WindowManager(1137):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
07-13 03:50:53.114: E/WindowManager(1137):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-13 03:50:53.114: E/WindowManager(1137):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-13 03:50:53.114: E/WindowManager(1137):  at android.app.Dialog.show(Dialog.java:241)
07-13 03:50:53.114: E/WindowManager(1137):  at game.location.AbstractActivity.showProgressDialog(AbstractActivity.java:39)
07-13 03:50:53.114: E/WindowManager(1137):  at game.location.LoginForm$1.onClick(LoginForm.java:43)
07-13 03:50:53.114: E/WindowManager(1137):  at android.view.View.performClick(View.java:2408)
07-13 03:50:53.114: E/WindowManager(1137):  at android.view.View$PerformClick.run(View.java:8816)
07-13 03:50:53.114: E/WindowManager(1137):  at android.os.Handler.handleCallback(Handler.java:587)
07-13 03:50:53.114: E/WindowManager(1137):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-13 03:50:53.114: E/WindowManager(1137):  at android.os.Looper.loop(Looper.java:123)
07-13 03:50:53.114: E/WindowManager(1137):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-13 03:50:53.114: E/WindowManager(1137):  at java.lang.reflect.Method.invokeNative(Native Method)
07-13 03:50:53.114: E/WindowManager(1137):  at java.lang.reflect.Method.invoke(Method.java:521)
07-13 03:50:53.114: E/WindowManager(1137):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-13 03:50:53.114: E/WindowManager(1137):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-13 03:50:53.114: E/WindowManager(1137):  at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>The code in doInBackGround:</p>

<pre><code>protected String doInBackground(String... arg0) {

            HttpPost httpPost = new HttpPost(Global.Url.LOGIN.getLink());
            String result = "";

            List&lt;NameValuePair&gt; pair = new ArrayList&lt;NameValuePair&gt;();
            pair.add(new BasicNameValuePair("userName", arg0[0]));
            pair.add(new BasicNameValuePair("password", arg0[1]));
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(pair));

            } catch (UnsupportedEncodingException e) {

                e.printStackTrace();
            }
            result = Util.sendPost(httpPost).toString();


            return result;
        }
</code></pre>

<p>The code that I have put all the necessary exception catching, where the exact error from:</p>

<pre><code>public static StringBuilder sendPost(HttpPost httpPost){
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = null;
        StringBuilder result = null;

        try {
            response = httpClient.execute(httpPost);

        } catch (ClientProtocolException e) {
            Log.e("Util.java, function sendPost", "ClientProtocolException encountered.");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("Util.java, function sendPost", "IOException encountered.");
            e.printStackTrace();
        }
        try {
            result = inputStreamToString(response.getEntity().getContent());
        } catch (IllegalStateException e) {
            Log.e("Util.java, function sendPost", "IllegalStateException encountered.");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("Util.java, function sendPost", "IOException encountered.");
            e.printStackTrace();
        }

        return result;
    }
</code></pre>

<p>Thank for any help.</p>
