1

I tried to build my first Android app, for the news-ticker of my school. I used Jsoup, but the app crashes every time. The problem occurs only on this webpage. When I enter a different url it works. Here is the link for the page: http://www.marianum24.de/~aushang/index.html And here is my code:

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

And finally my log:

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

I hope you can tell me where the problem lies.

  • Please do read the error log. It shows that you have a Null Pointer exception at line 199. – Rahul Gupta Nov 27 '13 at 17:02
  • provably doc.body() is null and you are catching only IOException. You have to see why. This thread might be useful to you http://stackoverflow.com/questions/10245519/handling-connection-errors-and-jsoup – mihail Nov 27 '13 at 17:04
  • i checked the connection, but the code is 200 and the message is "OK" –  Nov 27 '13 at 17:47

2 Answers2

0

Check MainActivity in line 199. There is something null there.

My bet is on this line:

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

I think doc might be null.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
0

Your problem appears to be a validation issue. Simply put, you are passing the raw HTML file to your Document object as if it were properly formatted XML. It isn't. According to the w3.org validation service,

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

at this time there are at least 8 errors and 1 warning with the website.

If I were you, I would run some checks to parse my input document BEFORE doing any processing on it.

chronodekar
  • 2,616
  • 6
  • 31
  • 36
  • i did now. `doc.body()` is `null` , something like `doc.getElementsByClass("MsoNormalTable")` is not `null` , but when i try `doc.getElementsByClass("MsoNormalTable").text()` , there is no content . `doc.text()` is also not null, but when i try to get the text, it only contains "aushang" . i cant get one single element, because the elements have no id. so what could i do? –  Nov 27 '13 at 18:41
  • it seems like i can get no text out of the body. but why? –  Nov 27 '13 at 19:06
  • Like I mentioned, your source XML is badly written. While HTML is supposed to be XML-compliant, most web-browsers have their own schemes of error correct that make this un-necessary. Why not just skip the element if you read NULL? – chronodekar Nov 28 '13 at 02:03
  • but the whole body is null and i cant get any valid element out of the Document. is there no possibility to get the data? –  Nov 28 '13 at 15:17