Android uses Jsoup to parse HTML development website Client Small note

Source: Internet
Author: User
Tags html page stub tostring trim

These days more spare time, idle to Nothing, think of the past has seen the development of any Web site client an article, that is, using Jsoup to resolve Web pages, through the label to get the content. Okay, no more nonsense, use the tool for Jsoup-1.7.2.jar package, specific Jsoup related documents, please go over here to see http://jsoup.org/, there are all APIs can be queried.

The site is a recipe site, the first analysis is a large category of column labels.

If you've ever used jquery then, the next thing is a piece of cake, we press F12 into the browser's developer mode, to analyze the structure of the current HTML page, the following figure:

The value in each <li></li> tag is what we need, and then we need to asynchronously get the Web page information, first create a connection, generate a Document object, get the ID value of <ul> of the title, and filter by SELECT.

Elements divs = Content.select ("#siteNav");

And then through the Jsoup to parse the label on the diagram, by looping out the title bar and subdirectories of hyperlinks, insert the database, hyperlinks used to expand the subdirectory of the Web page address, such as the large title structure of such a Web site is generally not changed, can be left as a cache, rather than each entry always to load parsing.

Get information asynchronously class Loadhtml extends Asynctask<string, String, string> {progressdialog bar;
        Document Doc;
            @Override protected string Doinbackground (String ... params) {//TODO auto-generated method stub
                 try {doc = Jsoup.connect (constans.netaddress). Timeout (5000). Post ();
                 Document content = Jsoup.parse (doc.tostring ());
                 Elements divs = Content.select ("#siteNav");
                 Document divcontions = Jsoup.parse (divs.tostring ());
                 Elements element = Divcontions.getelementsbytag ("Li");
                 LOG.D ("element", element.tostring ());
     
                     for (Element links:element) {String title = Links.getelementsbytag ("a"). Text ();
                     String link = links.select ("a"). attr ("href"). Replace ("/", ""). Trim ();
                     String url = constans.netaddress+link; ConTentvalues values = new Contentvalues ();
                     Values.put ("title", title);
                     Values.put ("url", url);
                 Usedatabase.insert ("Cach", values);
                The catch (IOException e) {//TODO auto-generated catch block
            E.printstacktrace ();
        return null; @Override protected void OnPostExecute (String result) {//TODO auto-generated method s
Tub Super.onpostexecute (result);
            LOG.D ("Doc", doc.tostring (). Trim ());
            Bar.dismiss ();
            Listitemadapter adapter = new Listitemadapter (context, usedatabase.getlist ());
        Listmenu.setadapter (adapter);
            @Override protected void OnPreExecute () {//TODO auto-generated method stub
            
            Super.onpreexecute ();
   bar = new ProgressDialog (context);         Bar.setmessage ("Loading Data");
            Bar.setindeterminate (FALSE);
            Bar.setcancelable (FALSE);
        Bar.show (); }
            
    }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.