Katana24
Katana24

Reputation: 8959

android and jsoup trouble

I'm trying to use JSoup in my android app to parse a certain website. However I don't seem to be getting anywhere. I've added the .jar of jsoup to the class path and tried to follow the examples on the JSoup website resource, the cookbook.

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView( R.layout.jsoup_layout );

    Toast.makeText( getApplicationContext(), "Hello World", Toast.LENGTH_SHORT);

    try {
        Document doc = Jsoup.connect( "http://en.wikipedia.org/wiki/Main_Page" ).get();
        Elements pTag = doc.select( "p" );

        String pTagString = pTag.html();
        Toast.makeText( getApplicationContext(), pTagString, Toast.LENGTH_SHORT);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Toast.makeText( getApplicationContext(), myString, Toast.LENGTH_SHORT );

}

So from this I'm trying to get the p tags of the wikipedia website. There are 12 or so in total but I only really want to display the value of one at this stage. But my app won't do anything. Even the first toast message meant to just display a message doesn't appear - this was only a check to see if it was working. So does anyone know what the problem is? Am i following the current syntax by choosing:

Elements pTag = doc.select( "p" );

Upvotes: 0

Views: 340

Answers (1)

MByD
MByD

Reputation: 137432

You should not connect to a website on the main thread! Use AsyncTask for such an operation.

Also, to display a Toast, you need to call show():

Toast
    .makeText( getApplicationContext(), "Hello World", Toast.LENGTH_SHORT)
        .show();

Upvotes: 1

Related Questions