Tuesday 2 September 2014

Fetch Data From A Website Using JSOUP in Android Application



How to fetch data from a website using JSOUP in Android Application :


JSOUP is a Java library for fetching the static html pages of a website. It has some limitations in the dynamic websites but it can fetch data from different websites quite easily and quickly than other tools.
You can fetch the whole page html content of any website through this library. Further information you can get from their site : JSOUP

I strongly recommend you to use this library for fetching the data from a website in your android application.

I will show you working example of JSOUP library in your android application :


In the above screenshot, I fetched the title of my blog and all the blog posts title which are present on the first page through the use of JSOUP library.

All you do is to right click on the site you want to fetch the data and go to view page source and than search for the content in the page source you want to use in your android application.


In my case it is the h1 tag for fetching the title of my blog and h3 tag for fetching the title of each post that’s all I wanted to show in an example like you saw above.

     
     
Try to use the tag you want to fetch in the selector statement of JSOUP which is very powerful or else try to start with its parent. In my application I can fetch the title of my blog directly with the h1 tag but for h3 tag I used its parent div tag for fetching.

Now lets start with the example. Create a new Project in Eclipse : File => New => Android Application Project and name it you want and give it a package as com.javalanguageprogramming.jsoupdemo.

Start your project by copying the JSOUP library file in your libs folder . The library file of JSOUP is here : JSOUP Library

Code of activity_main.xml is shown below :

 <LinearLayout 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:orientation="vertical"  
   >  
   
   <LinearLayout   
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:orientation="horizontal"  
     >  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="TITLE :"   
     android:textSize="20sp"  
     android:textStyle="bold"  
     android:layout_marginLeft="15px"  
     android:layout_marginTop="10px"  
       
     />  
   <TextView  
     android:id="@+id/titleText"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:textSize="18sp"  
     android:layout_marginLeft="15px"  
     android:layout_marginTop="10px"  
     />  
   
   </LinearLayout>  
     
     
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="POSTS TITLE :"   
     android:textSize="20sp"  
     android:textStyle="bold"  
     android:layout_marginLeft="15px"  
     android:layout_marginTop="10px"  
       
     />  
   <TextView  
     android:id="@+id/postsText"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:textSize="15sp"  
     android:layout_marginLeft="15px"  
     android:layout_marginTop="10px"  
     />   
 </LinearLayout>  
   


In this code we are making four text views two text views we directly used for headings and the other two are used when through the use of JSOUP we get the data from my blog. So, I gave them Id’s for binding with my MainActivity.java.

Code for MainActivity.java is shown below :

 package com.javalanguageprogramming.jsoupdemo;  
   
 import org.jsoup.Jsoup;  
 import org.jsoup.nodes.Document;  
 import org.jsoup.select.Elements;  
   
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.widget.TextView;  
   
   
 public class MainActivity extends Activity {  
   
      TextView titleText, postText;  
      String title, posts = "";  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
             
           //initialize variables  
           titleText = (TextView)findViewById(R.id.titleText);  
           postText = (TextView)findViewById(R.id.postsText);  
             
             
           //run on new thread because we cannot do network operation on main thread  
           new Thread(new Runnable() {  
                  
                @Override  
                public void run() {  
                     try{  
                            
                     //get the Document object from the site. Enter the link of site you want to fetch  
                     Document document = Jsoup.connect("http://javalanguageprogramming.blogspot.in/").get();  
                       
                     //Get the title of blog using title tag  
                     title = document.select("h1.title").text().toString();  
                     //set the title of text view  
                       
                       
                     //Get all the elements with h3 tag and has attribute a[href]  
                     Elements elements = document.select("div.post-outer").select("h3").select("a[href]");  
                     int length = elements.size();  
                       
                     for(int i=0; i<length; i++){  
                          //store each post heading in the string  
                          posts += elements.get(i).text() + "\n\n";  
                            
                     }  
                       
                     //Run this on ui thread because another thread cannot touch the views of main thread  
                     runOnUiThread(new Runnable() {  
                            
                          @Override  
                          public void run() {  
                                 
                               //set both the text views  
                               titleText.setText(title);  
                               postText.setText(posts);  
                          }  
                     });  
                       
                }catch(Exception e){  
                     e.printStackTrace();  
                }  
                }  
           }).start();  
             
             
 }  
 }  
   


I made comments where I fetched the data from the website in MainActivity.java. You can also refer to JSOUP site if you want to fetch other types of tags.

Do not forget to add uses-permission : Internet because this application must use internet for fetching data from websites.

The AndroidManifest file is shown below :

 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.javalanguageprogramming.jsoupdemo"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="19" />  
   <uses-permission android:name="android.permission.INTERNET" />  
   
   
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.javalanguageprogramming.jsoupdemo.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
   
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
   
 </manifest>  
   

Note : It takes 3-4 sec to fetch and show the data to your application, at that time you can use progress dialog.

12 comments :

  1. Great tutorisl boss...Thank you..
    But If I want to read the posts then what i have to do??

    ReplyDelete
  2. Posts shared useful information and meaningful life, I'm glad to be reading this article and hope to soon learn the next article. thank you
    i like play games friv4 online and play games girls, Download facebook movel

    ReplyDelete
  3. Thanks for sharing the information. It is very useful for my future. keep sharing. Can you play more games at: Friv10000 | Kizi 4 | happy wheels

    ReplyDelete
  4. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.

    python Training in Bangalore | python Training in Bangalore

    ReplyDelete
  5. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end. 
    microsoft azure training in bangalore
    rpa training in bangalore
    best rpa training in bangalore

    ReplyDelete
  6. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard
    Best Devops Training institute in Chennai

    ReplyDelete
  7. It is better to engaged ourselves in activities we like. I liked the post. Thanks for sharing.
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  8. Attend The Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Bangalore.

    ReplyDelete
  9. Take a class for Python Training within Hyderabad and begin your bright career in this field by enrolling in AI Patasala. Students can pursue their careers through this Python course.
    Python Training in Hyderabad

    ReplyDelete
  10. This post is so interactive and informative.keep update more information...
    SEO Training in Velachery
    SEO Training in Chennai

    ReplyDelete
  11. Vidal International's Data Science Course in Vizag offers comprehensive training in the field of data science. Learn essential skills such as data analysis, machine learning, and predictive modeling from industry experts. Gain hands-on experience through practical projects and develop the expertise to extract valuable insights from complex datasets. Join our course and embark on a rewarding journey in the world of data science.

    ReplyDelete