0

After many tries, I decided to ask the question again. In my last question, someone said I should have a look at Jsoup. I wrote some code but it won't work. It's an android app. But it totally crashes. with the error message:

Unfortunately, (appname) has stopped

See the full error message

My code for extracting text from the <div>:

 public void ButtonClick(View view) throws IOException {
    Document doc = dereference("here is my url");
    String text = extractContent(doc);
    updateUI(text);
}

private Document dereference(String uri) {
    Connection connection = Jsoup.connect(uri);
    return connection.get();
}

private String extractContent(Document doc) {
    Elements divs = doc.select("div.onlinestatus");
    return divs.text();
}

private void updateUI(String text) {
    TextView tv = (TextView)findViewById(R.id.textView1);
    tv.setText(text);
}

the input from the url:

<html><!-- [...] --><body>
    <div class='onlinestatus'>Server ist online! <br /></div>
</body></html>

Can someone spot the mistake?

Edit: when I perform all these operations in a separate thread, I get a different error. Error log and code can be found here.

Community
  • 1
  • 1
Phil
  • 943
  • 2
  • 6
  • 18
  • Try to add more information to this question: Link to your previous question, what your input is, what "totally crashes" means, anything else that's relevant. Also, try to work with unit tests to verify the workings of chunks of code. – derabbink Nov 02 '12 at 16:57
  • My Input http://web97.confixx.harzkreativ.de/skycraft/mc.php ; My last thread: http://stackoverflow.com/questions/13196126/reading-data-from-a-website-with-http-get-and-regular-expression ; I tested it without the textview, it crashes too :/ totaly crashes means error message "Unfortunately, (appname) has stopped" – Phil Nov 02 '12 at 17:01
  • You are, in fact, doing three things in this code: Dereferencing a URI (i.e. getting the HTML text), extracting something from that HTML string, and then manipulating a UI element. The error could arise from any one of these parts. try isolating each part and find out which one exactly fails. – derabbink Nov 02 '12 at 17:28
  • Hm it looks like it fails at the first part, where the connect is. When I `//` the 2 other parts it gives me the error message like without the `//` – Phil Nov 02 '12 at 17:36
  • I tried it now with an extra thread but always get this error message as above :/ – Phil Nov 02 '12 at 18:17
  • Hm i have some new information... if I do this in a new thread, there's no error message but only if I `//` the setText line, but I don't know why :/ I replaced the `divs.text()` through some text but doesn't work either – Phil Nov 02 '12 at 18:32

1 Answers1

0

This is not a Jsoup question after all.

If you look up the error from your error log (first line) where it reads

NetworkOnMainThreadException

Googling for "android NetworkOnMainThreadException" yiedls a page from the android developer reference, which states

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This would explain your previous attempts at moving code into a separate thread, which seemed to produce different results.
Have a look at the page suggested in the android developer reference, on Designing for Responsiveness. That should give you an answer.

The next thing that can fail, is that you need to do all UI changes in the thread that created the UI. The CalledFromWrongThreadException you got here tells you exactly what went wrong. Looking up that error will lead you to a question+answer similar to this one.

If you have problems with that, I suggest you ask a new question, if your problem is not already covered on StackOverflow (but I believe it is!)

Community
  • 1
  • 1
derabbink
  • 2,419
  • 1
  • 22
  • 47
  • I tried the `.` but it doesnt work. Then I changed the class to id, changed the `.` to `#` but it doenst work :/ – Phil Nov 02 '12 at 17:18
  • Have you tried changing the single quotes (') to double quotes (")? Try to test this code as a (JUnit) unit test. A testing framework allows you to easier run variations of code at once, and it also might give you clearer error message back (like an underlying exception). If you don't want that, try to catch any exception on each line of this code. – derabbink Nov 02 '12 at 17:22
  • Yes I did it now, but nothing other happens :/ How can I test it as JUnit? :o I'm new to Java and didn't need this until now :o – Phil Nov 02 '12 at 17:28
  • Take a look at the now edited answer. Using the debugger of your IDE (e.g. Eclipse) you can run the program step-by-step and see what parts it successfully executes before it crashes. Please report back. Explaining how to set up unit testing goes beyond the scope of this questions answer, and you can find plenty of information on that on the internet, including this site. – derabbink Nov 02 '12 at 20:40
  • So now I now it is because of the first part, the connect. It says `HttpConnection$Response.execute(Connection$Request, HttpConnection$Response) line: 456`, many other things and `The JAR of this class file belongs to container 'Android Dependencies' which does not allow modifications to source attachments on its entries.` – Phil Nov 03 '12 at 07:23
  • Please use the yet again updated code from the answer. Please report back the **complete error/output**, by copy-pasting it into e.g. pastebin.com and providing the link here. – derabbink Nov 03 '12 at 09:01
  • Hope this helps, dont really know if this is what u mean :o http://pastebin.com/uMN2xa8w – Phil Nov 03 '12 at 09:20
  • That output was essential! In the future, consider providing such detail in your initial question right from the start. I have edited your question to accurately reflect the _real_ problem (approval pending), and I edited my answer accordingly. – derabbink Nov 03 '12 at 09:51
  • Ok, I have something new. The app doesnt crash totally anymore, but nothing happens :/ New error log + code: http://pastebin.com/PbTvMHsW Thanks for your great help (: – Phil Nov 03 '12 at 10:18
  • Have a look at the now updated question and answer. I think this question is resolved now. IF you agree, you can accept the answer. If new/different problems should arise, consider asking a new question. As with methods in your code, StackOverflow questions should have one concern only. – derabbink Nov 03 '12 at 11:19