-1

HttpHostConnectException当用户尝试在没有互联网连接的情况下发送一些数据时,我想处理。我在连接到服务器的 RestClient 类中这样做:

catch (HttpHostConnectException e)  {

                client.getConnectionManager().shutdown();
                try {
                    throw new  NoInternetException();
                } catch (NoInternetException e1) {
                    inetError = true;
                    e1.printStackTrace();
                }

                e.printStackTrace();
            } 

当我发现异常时,在我的活动中:

catch(NoInternetException  e) {
            this.cancel(true);
            editor.clear();
            editor.commit();
            finish();
            startActivity(getIntent());
             Toast.makeText(LoginActivity.this, "You've lost internet connection. You should try later.",Toast.LENGTH_LONG)
               .show();
            e.printStackTrace();
            }

我想处理这个异常,重新启动用户上次使用的当前活动并显示 Toast,例如“你没有任何连接”。但是我得到了带有强制关闭按钮的异常弹出窗口。我不希望它显示,但想重新加载 Activity 并用我的消息显示 Toast。我该如何实施?

异常的堆栈跟踪:

 com.library.NoInternetException
    at com.library.RestClient$AsyncExecute.doInBackground(RestClient.java:447)
    at com.library.RestClient$AsyncExecute.doInBackground(RestClient.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    at java.lang.Thread.run(Thread.java:1019)
org.apache.http.conn.HttpHostConnectException: Connection to https://192.168.0.250 refused
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    at com.library.RestClient$AsyncExecute.doInBackground(RestClient.java:401)
    at com.library.RestClient$AsyncExecute.doInBackground(RestClient.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    at java.lang.Thread.run(Thread.java:1019)
 Caused by: java.net.ConnectException: /192.168.0.250:443 - Network is unreachable
    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
    at java.net.Socket.connect(Socket.java:1002)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
... 14 more
 Error converting result java.lang.NullPointerException
 End of input at character 0 of 
 java.lang.InterruptedException
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1272)
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:220)
    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
    at android.os.AsyncTask$3.done(AsyncTask.java:196)
    at java.util.concurrent.FutureTask$Sync.innerCancel(FutureTask.java:294)
    at java.util.concurrent.FutureTask.cancel(FutureTask.java:76)
    at android.os.AsyncTask.cancel(AsyncTask.java:325)
    at com.assignmentexpert.LoginActivity$LoginTask.doInBackground(LoginActivity.java:422)
    at com.assignmentexpert.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    at java.lang.Thread.run(Thread.java:1019)
 cscCountry is not German : SEK
 dashboard
    10-15 18:48:02.469: I/download page count dashboard(5061): 1
    10-15 18:48:02.559: D/myLogs(5061): onCreate
    10-15 18:48:02.559: D/myLogs(5061): onStartCommand
    10-15 18:48:02.839: I/getOrders start(5061): 1
    10-15 18:48:03.039: I/getOrders end(5061): 6
    10-15 18:48:03.039: I/params(5061): page1
    10-15 18:48:03.039: I/params(5061): perpage6
    10-15 18:48:03.039: W/dalvikvm(5061): threadid=13: thread exiting with uncaught exception (group=0x40018578)
    10-15 18:48:03.089: E/AndroidRuntime(5061): FATAL EXCEPTION: AsyncTask #4
java.lang.RuntimeException: An error occured while executing doInBackground()

    at android.os.AsyncTask$3.done(AsyncTask.java:200)
    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
    10-15 18:48:03.089: E/AndroidRuntime(5061):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    at java.lang.Thread.run(Thread.java:1019)
 Caused by: java.lang.NullPointerException
    at com.library.ServiceMessages.md5(ServiceMessages.java:387)
    at com.library.ServiceMessages$DklabExecute.doInBackground(ServiceMessages.java:114)
    at com.library.ServiceMessages$DklabExecute.doInBackground(ServiceMessages.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    ... 4 more
    10-15 18:48:03.169: I/database closing(5061): All DAO's might be destroyed
 destroy
com.library.NoInternetException
    at com.library.RestClient$AsyncExecute.doInBackground(RestClient.java:447)
    at com.library.RestClient$AsyncExecute.doInBackground(RestClient.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    at java.lang.Thread.run(Thread.java:1019)
org.apache.http.conn.HttpHostConnectException: Connection to https://192.168.0.250 refused
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    at com.library.RestClient$AsyncExecute.doInBackground(RestClient.java:401)
    at com.library.RestClient$AsyncExecute.doInBackground(RestClient.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    at java.lang.Thread.run(Thread.java:1019)
Caused by: java.net.ConnectException: /192.168.0.250:443 - Network is unreachable
    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
    at java.net.Socket.connect(Socket.java:1002)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
    ... 14 more
 Error converting result java.lang.NullPointerException
Error parsing data org.json.JSONException: End of input at character 0 of 
 java.lang.NullPointerException
    at com.library.UserFunctions.getOrders(UserFunctions.java:71)
    at com.assignmentexpert.DashboardActivityAlt.getOrderList(DashboardActivityAlt.java:318)
    at com.assignmentexpert.DashboardActivityAlt.access$6(DashboardActivityAlt.java:309)
    at com.assignmentexpert.DashboardActivityAlt$DownloadTask.doInBackground(DashboardActivityAlt.java:500)
    at com.assignmentexpert.DashboardActivityAlt$DownloadTask.doInBackground(DashboardActivityAlt.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
        at java.lang.Thread.run(Thread.java:1019)
4

2 回答 2

0

你得到强制关闭是因为你可能在你的 UI 线程中运行这个 HttpConnection。这是有问题的,因为它会导致 UI 挂起和卡顿……因此,强制关闭。改用一个AsyncTask

于 2012-10-15T16:06:38.527 回答
0

那里只有 3 行重要的错误:

Error parsing data org.json.JSONException: End of input at character 0 of 
10-15 18:48:03.759: W/System.err(5061): java.lang.NullPointerException
10-15 18:48:03.759: W/System.err(5061):     at com.library.UserFunctions.getOrders(UserFunctions.java:71)

您似乎得到了一个空响应,或者至少是一个无法作为 JSON 线程化的响应。检查UserFunctions第 71 行并检查您的 AsyncTask 是否可以处理非 JSON 结果而不会崩溃。

于 2012-10-15T16:18:43.393 回答