As promised, we will now look at using Jsoup with Android. This is pretty simple and very similar to the previous java example where we parsed the title from Wikipedia’s page. By keeping it bare bones and simple you should see how this works and be able to add more to it gradually.

Start by making a new Android project with a blank activity and blank layout. Add the Jsoup .jar into the libs directory of your project and add to the build path.

Get the quickest/easy thing out the way by putting the following in your manifest.xml file beneath the uses SDK section. We are going to be using the device’s Internet connection so you need a permission to do so.

  <uses-permission android:name="android.permission.INTERNET"/>

Copy the code below for your layout file. It is just a button and a textView with IDs so we can access them from the main activity.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.jsouptest.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:text="TextView" />

</RelativeLayout>

Now change your Main Activity to look like the below. I have commented in the code to try and explain what is going on.

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.support.v7.app.ActionBarActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	String url = "http://www.wikipedia.org";
	Document doc = null;
	TextView textView = null;

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

        textView = (TextView) findViewById(R.id.textView1);
        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				textView.setText("WORKING"); //just to show button has been pressed
				new DataGrabber().execute(); //execute the asynctask below
			}
		});

    }
    //New class for the Asynctask, where the data will be fetched in the background
    private class DataGrabber extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			// NO CHANGES TO UI TO BE DONE HERE
			try {
				doc = Jsoup.connect(url).get();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			//This is where we update the UI with the acquired data
			if (doc != null){
				textView.setText(doc.title().toString());
			}else{
				textView.setText("FAILURE");
			}
		}
    }
}

I suppose the main thing that is out of the ordinary here is having a new class (DataGrabber) that extends AsyncTask. The reason is that if you want to get data from a server or the internet like we are you cannot do it from the main UI thread. This is so that if it takes some time it won’t make the UI unresponsive. It is quite simple though, as you see I have just used the method doInBackground to get the URL and then used onPostExecute to modify my UI with the result (you can’t modify the UI from a background task!). More info on all that here if you are interested.

Run the app on a device with Internet access and if all has worked well you should see something like this:

Screenshot_2014-08-10-21-54-48

Now experiment by trying to get other information out of other websites, like the last post. If you want to see this in action I have used it in an app of mine. Its pretty simple but does what it needs to.

Hopefully that was helpful. Let me know if anything needs greater detail.