当用户尝试在没有互联网连接的情况下发送某些数据时,我想处理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,例如“您没有任何连接”。但是我通过强制关闭按钮获得异常弹出窗口。我不希望它显示,但想要重新加载活动并显示我的消息Toast。我该如何实现呢?
异常的stacktrace:
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)
答案 0 :(得分:0)
你得到强制关闭,因为你可能在你的UI线程中运行这个HttpConnection。这是有问题的,因为它会导致UI挂起和口吃......因此,Force-Close。请改用AsyncTask
。
答案 1 :(得分: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结果而不会崩溃。