解析一个特定网站时Android应用程序崩溃

时间:2013-11-27 16:59:02

标签: java android android-asynctask crash jsoup

我尝试构建我的第一个Android应用程序,用于我学校的新闻自动收录器。我使用了Jsoup,但应用程序每次都崩溃了。问题仅发生在此网页上。当我输入不同的网址时,它可以正常工作。以下是该页面的链接:http://www.marianum24.de/~aushang/index.html 这是我的代码:

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

我希望你能告诉我问题所在。

2 个答案:

答案 0 :(得分:0)

检查第199行中的MainActivity。那里有null

我的赌注就在这条线上:

htmlString.append(doc.body().text());

我认为doc可能是null

答案 1 :(得分:0)

您的问题似乎是验证问题。简单地说,您将原始HTML文件传递给Document对象,就好像它是格式正确的XML一样。事实并非如此。根据w3.org验证服务,

http://validator.w3.org/check?uri=http%3A%2F%2Fwww.marianum24.de%2F~aushang%2Findex.html&charset=%28detect+automatically%29&doctype=Inline&group=0

此时网站至少有8个错误和1个警告。

如果我是你,我会在进行任何处理之前运行一些检查来解析我的输入文件。