-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();
            }

この例外を処理し、ユーザーが最後に使用した現在のアクティビティを再起動し、「接続がありません」などのトーストを表示したいと考えています。しかし、強制終了ボタンで例外ポップアップが表示されます。表示したくありませんが、アクティビティをリロードして、メッセージでトーストを表示したいです。どうすればこれを実装できますか?

例外のスタックトレース:

 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

重要なエラーは 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 としてスレッド化できない応答を取得しているようです。71 行目を確認UserFunctionsし、AsyncTask が非 JSON の結果をクラッシュせずに処理できることを確認します。

于 2012-10-15T16:18:43.393 に答える
0

おそらく UI スレッドでこの HttpConnection を実行しているため、Force Close が発生します。UI がハングアップしてカクカクするので、これは問題です。つまり、強制終了です。AsyncTask代わりにを使用してください。

于 2012-10-15T16:06:38.527 に答える