Using Jsoup on Android
Hello everyone!
As Android developers, we often need to call web services in order to fetch data from a server. We also need to parse the data for displaying it on the user interface. There are various parsers available that enable developers to retrieve data efficiently from a web service.
JSoup is one such open source library that provides an API for extracting and manipulating data. Basically, it is an HTML parser used for working with various HTML elements, attributes etc. It can also find and extract data, using DOM traversal or CSS selectors.
Through this post, we will learn how to use the JSoup library in Android
Pre-requisites: Eclipse IDE, Android SDK
Step 1: Let's first create a new Android application project called AndroidJSoupDemo with package name com.app.jsoupexample
JSoup implements the WHATWG HTML5 specification, and parses HTML to the same DOM as modern browsers do. In this case, we will parse an HTML page and display the data on the user interface.
Step 2: Create Activity class
Create the MainActivity class to parse HTML from a URL, file, or string. In this case, we have chosen a URL. JSoup will help create a sensible parse tree.
MainActivity.java
package com.app.jsoupexample;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final String URL = "<your-website-url-goes-here>";
ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnFetchData = (Button) findViewById(R.id.btnData);
btnFetchData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new FetchWebsiteData().execute();
}
});
}
private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {
String websiteTitle, websiteDescription;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to website
Document document = Jsoup.connect(URL).get();
// Get the html document title
websiteTitle = document.title();
// Get the description element
Elements description = document.select("meta[name=description]");
// Locate the content attribute
websiteDescription = description.attr("content");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set title into TextView
TextView txttitle = (TextView) findViewById(R.id.txtData);
txttitle.setText(websiteTitle + "\n" + websiteDescription);
mProgressDialog.dismiss();
}
}
}
activity_main.xml
<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" >
<TextView
android:id="@+id/txtData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<Button
android:id="@+id/btnData"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtData"
android:text="@string/data" />
</RelativeLayout>
Note: Before running the application, make sure you add the android.permission.INTERNET in your AndroidManifest.xml file.
Save all changes. Make sure no errors are present. Run the project on an Android device and you should see the following output!