Tuesday, 18 November 2014

JSOUP Example Android

JSOUP Implementation for Android.

Basic Ideas about JSOUP:
JSOUP is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.JSOUP implements the WHATWG HTML5 specification, and parses HTML to the same DOM as modern browsers do.
JSOUP allows you to scrape and parse HTML from a URL, file, or string and many more.

Before Starting development, Download JSOUP form this URL: http://jsoup.org/download


Now Create a New Android Project. Here I am creating a new project named "TestJSOUP".
In that project, you must have to put jsoup rar file inside your libs folder.
After copying file, you have to build path inside your project,rightclick on jsoup.rar->Buildpath->Add to Build Path.

Our Main intensity of this project is get Title, Document name and image. Here for example I am taking one website "http://www.dreamstime.com/" and
fetching the data.
We can get the title by using document.title().
Just Write Down this bellow code and execute this in your system.

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="website title" />
    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tv_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

MainActivity.java
public class MainActivity extends ActionBarActivity {

String url = "http://www.dreamstime.com/";

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

wbTitle = (Button)findViewById(R.id.btn_title);
img = (ImageView)findViewById(R.id.img);

wbTitle.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
new Title().execute();
}
});
}

private class Title extends AsyncTask<Void, Void, Void>{

ProgressDialog pd;
String title;

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Loading Your Page");
pd.setMessage("Loading...");
            pd.setIndeterminate(false);
            pd.show();
}

@Override
protected Void doInBackground(Void... arg0) {
try {
Document document = Jsoup.connect(url).get();
title = document.title();
Elements elem = document.select("meta[name=description]");
Elements img = document.select("a[id=frontLink] img[src]");
String descriptionContent = elem.attr("content");
String imgPath = img.attr("src");
System.out.println("ttitle:"+title);
System.out.println("Description: "+descriptionContent);
System.out.println("ImagePath=>"+imgPath);
System.out.println("iimmgg:"+img);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
TextView txttitle = (TextView) findViewById(R.id.tv_details);
            txttitle.setText(title);
pd.dismiss();
}

}

}


1 comment: