我正在成功访问 asmx Web 服务,但是当没有互联网时,应用程序崩溃。我已经尝试了所有方法来检测和处理异常,但都是徒劳的。
请检查我的代码和 logcat。
日志猫:
10-13 14:22:34.748: W/System.err(275): at java.lang.Thread.run(Thread.java:1096)
10-13 14:22:34.759: W/dalvikvm(275): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
10-13 14:22:34.858: D/dalvikvm(275): GC_FOR_MALLOC freed 4242 objects / 236920 bytes in 62ms
10-13 14:22:34.858: E/AndroidRuntime(275): FATAL EXCEPTION: AsyncTask #1
10-13 14:22:34.858: E/AndroidRuntime(275): java.lang.RuntimeException: An error occured while executing doInBackground()
10-13 14:22:34.858: E/AndroidRuntime(275): at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-13 14:22:34.858: E/AndroidRuntime(275): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-13 14:22:34.858: E/AndroidRuntime(275): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-13 14:22:34.858: E/AndroidRuntime(275): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-13 14:22:34.858: E/AndroidRuntime(275): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-13 14:22:34.858: E/AndroidRuntime(275): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-13 14:22:34.858: E/AndroidRuntime(275): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-13 14:22:34.858: E/AndroidRuntime(275): at java.lang.Thread.run(Thread.java:1096)
10-13 14:22:34.858: E/AndroidRuntime(275): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
10-13 14:22:34.858: E/AndroidRuntime(275): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.view.View.requestLayout(View.java:8125)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.view.View.requestLayout(View.java:8125)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.view.View.requestLayout(View.java:8125)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.view.View.requestLayout(View.java:8125)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.view.View.requestLayout(View.java:8125)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.widget.TextView.checkForRelayout(TextView.java:5371)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.widget.TextView.setText(TextView.java:2688)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.widget.TextView.setText(TextView.java:2556)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.widget.TextView.setText(TextView.java:2531)
10-13 14:22:34.858: E/AndroidRuntime(275): at com.bibleverses.BibleverseActivity$ws.doInBackground(BibleverseActivity.java:190)
10-13 14:22:34.858: E/AndroidRuntime(275): at com.bibleverses.BibleverseActivity$ws.doInBackground(BibleverseActivity.java:1)
10-13 14:22:34.858: E/AndroidRuntime(275): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-13 14:22:34.858: E/AndroidRuntime(275): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-13 14:22:34.858: E/AndroidRuntime(275): ... 4 more
10-13 14:22:35.158: W/IInputConnectionWrapper(275): showStatusIcon on inactive InputConnection
10-13 14:22:36.009: E/WindowManager(275): Activity com.bibleverses.BibleverseActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f14620 that was originally added here
10-13 14:22:36.009: E/WindowManager(275): android.view.WindowLeaked: Activity com.bibleverses.BibleverseActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f14620 that was originally added here
10-13 14:22:36.009: E/WindowManager(275): at android.view.ViewRoot.<init>(ViewRoot.java:247)
10-13 14:22:36.009: E/WindowManager(275): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
10-13 14:22:36.009: E/WindowManager(275): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-13 14:22:36.009: E/WindowManager(275): at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-13 14:22:36.009: E/WindowManager(275): at android.app.Dialog.show(Dialog.java:241)
10-13 14:22:36.009: E/WindowManager(275): at com.bibleverses.BibleverseActivity$ws.onPreExecute(BibleverseActivity.java:240)
10-13 14:22:36.009: E/WindowManager(275): at android.os.AsyncTask.execute(AsyncTask.java:391)
10-13 14:22:36.009: E/WindowManager(275): at com.bibleverses.BibleverseActivity$1.onClick(BibleverseActivity.java:107)
10-13 14:22:36.009: E/WindowManager(275): at android.view.View.performClick(View.java:2408)
10-13 14:22:36.009: E/WindowManager(275): at android.view.View$PerformClick.run(View.java:8816)
10-13 14:22:36.009: E/WindowManager(275): at android.os.Handler.handleCallback(Handler.java:587)
10-13 14:22:36.009: E/WindowManager(275): at android.os.Handler.dispatchMessage(Handler.java:92)
10-13 14:22:36.009: E/WindowManager(275): at android.os.Looper.loop(Looper.java:123)
10-13 14:22:36.009: E/WindowManager(275): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-13 14:22:36.009: E/WindowManager(275): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 14:22:36.009: E/WindowManager(275): at java.lang.reflect.Method.invoke(Method.java:521)
10-13 14:22:36.009: E/WindowManager(275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-13 14:22:36.009: E/WindowManager(275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-13 14:22:36.009: E/WindowManager(275): at dalvik.system.NativeStart.main(Native Method)
10-13 14:22:40.798: I/Process(275): Sending signal. PID: 275 SIG: 9
代码:
private class ws extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
// try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String bk = ed.getText().toString();
tx1 = (EditText) findViewById(R.id.editText1);
String Chapter = tx1.getText().toString();
int chp = Integer.parseInt(Chapter);
EditText tx2 = (EditText) findViewById(R.id.editText2);
String verse = tx2.getText().toString();
int vs = Integer.parseInt(verse);
//
//
request.addProperty("BookTitle", bk);
request.addProperty("chapter", chp);
request.addProperty("Verse", vs);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER12);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tv.setText(e.toString());
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object result = null;
try {
result = (Object) envelope.getResponse();
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String html = result.toString();
Document doc = Jsoup.parse(html);
Elements result2 = doc.select("biblewords");
String result3 = result2.toString();
String res = Html.fromHtml(result3).toString();
return res;
}
@Override
protected void onPostExecute(String res) {
dialog.dismiss();
tv.setText(res); // txt.setText(result);
}