我試圖構建我的第一個Android應用程序,用於我學校的新聞發佈。我使用Jsoup,但每次都會崩潰。這個問題只發生在這個網頁上。當我輸入不同的網址時,它就可以工作。這裏是鏈接的頁面:http://www.marianum24.de/~aushang/index.html 這裏是我的代碼:解析某個特定網站時Android應用程序崩潰
private String loadXmlFromNetwork(String urlString) throws IOException {
Document doc = null;
StringBuilder htmlString = new StringBuilder();
try {
doc = Jsoup.connect(urlString).get();
htmlString.append(doc.body().text());
} catch (IOException e) {
return getResources().getString(R.string.connection_error);
}
return htmlString.toString();
}
最後我的日誌:
11-27 11:30:33.081: D/gralloc_goldfish(5227): Emulator without GPU emulation detected.
11-27 11:30:35.251: D/dalvikvm(5227): GC_FOR_ALLOC freed 149K, 9% free 2768K/3024K, paused 51ms, total 51ms
11-27 11:30:36.292: D/dalvikvm(5227): GC_FOR_ALLOC freed 398K, 15% free 2881K/3380K, paused 50ms, total 51ms
11-27 11:30:37.411: D/dalvikvm(5227): GC_FOR_ALLOC freed 181K, 9% free 3215K/3496K, paused 59ms, total 59ms
11-27 11:30:37.952: W/dalvikvm(5227): threadid=12: thread exiting with uncaught exception (group=0x414c4700)
11-27 11:30:37.971: E/AndroidRuntime(5227): FATAL EXCEPTION: AsyncTask #1
11-27 11:30:37.971: E/AndroidRuntime(5227): java.lang.RuntimeException: An error occured while executing doInBackground()
11-27 11:30:37.971: E/AndroidRuntime(5227): at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-27 11:30:37.971: E/AndroidRuntime(5227): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
11-27 11:30:37.971: E/AndroidRuntime(5227): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
11-27 11:30:37.971: E/AndroidRuntime(5227): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
11-27 11:30:37.971: E/AndroidRuntime(5227): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-27 11:30:37.971: E/AndroidRuntime(5227): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-27 11:30:37.971: E/AndroidRuntime(5227): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-27 11:30:37.971: E/AndroidRuntime(5227): at java.lang.Thread.run(Thread.java:841)
11-27 11:30:37.971: E/AndroidRuntime(5227): Caused by: java.lang.NullPointerException
11-27 11:30:37.971: E/AndroidRuntime(5227): at com.boldog.aushang.MainActivity.loadXmlFromNetwork(MainActivity.java:199)
11-27 11:30:37.971: E/AndroidRuntime(5227): at com.boldog.aushang.MainActivity.access$0(MainActivity.java:192)
11-27 11:30:37.971: E/AndroidRuntime(5227): at com.boldog.aushang.MainActivity$DownloadXmlTask.doInBackground(MainActivity.java:175)
11-27 11:30:37.971: E/AndroidRuntime(5227): at com.boldog.aushang.MainActivity$DownloadXmlTask.doInBackground(MainActivity.java:1)
11-27 11:30:37.971: E/AndroidRuntime(5227): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-27 11:30:37.971: E/AndroidRuntime(5227): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-27 11:30:37.971: E/AndroidRuntime(5227): ... 4 more
11-27 11:30:42.774: I/Choreographer(5227): Skipped 78 frames! The application may be doing too much work on its main thread.
11-27 11:31:28.801: I/Process(5227): Sending signal. PID: 5227 SIG: 9
我希望你能告訴我問題所在。
請仔細閱讀錯誤日誌。它顯示你在第199行有一個空指針異常。 –
可證明doc.body()爲空,並且只捕獲IOException。你必須明白爲什麼。此線程可能對您有用http://stackoverflow.com/questions/10245519/handling-connection-errors-and-jsoup – mihail
我檢查了連接,但代碼是200,並且消息是「確定」 – user3042432