Android Development uses Jsoup to parse HTML pages _android

Source: Internet
Author: User

The examples in this article describe how Android uses Jsoup to parse HTML pages. Share to everyone for your reference, specific as follows:

This section is mainly to explain Jsoup parsing HTML pages. Because in the Android development process, inevitably involves the Web page crawl, parsing, display and so on, so, here I mainly show the use of Jsoup jar package to crawl cnbeta.com site of the topic classification instances.

Here is the main code, because of the use and its simplicity, I will not say more:

Package com.android.web;
Import Java.io.BufferedInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import java.net.URLConnection;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Org.apache.http.util.ByteArrayBuffer;
Import Org.apache.http.util.EncodingUtils;
Import Org.jsoup.Jsoup;
Import org.jsoup.nodes.Document;
Import org.jsoup.nodes.Element;
Import org.jsoup.select.Elements;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
  public class _getwebresoureactivity extends activity {Document doc;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
     Findviewbyid (R.id.button1). Setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) {load ();
  }
    });
    protected void Load () {try {doc = Jsoup.parse (new URL ("http://www.cnbeta.com"), 5000);
    catch (Malformedurlexception E1) {e1.printstacktrace ();
    catch (IOException E1) {e1.printstacktrace ();
    } list<map<string, string>> List = new arraylist<map<string, string>> ();
    Elements es = Doc.getelementsbyclass ("Main_navi");
      for (Element e:es) {map<string, string> Map = new hashmap<string, string> ();
      Map.put ("title", E.getelementsbytag ("a"). text ());
      Map.put ("href", "http://www.cnbeta.com" + E.getelementsbytag ("a"). attr ("href"));
    List.add (map);
    } ListView ListView = (ListView) Findviewbyid (R.ID.LISTVIEW1); Listview.setadapter New Simpleadapter (this, list, Android. R.layout.simple_list_item_2, new string[] {"title", "href"}, new int[] {Android. R.id.text1,android. R.ID.TEXT2}));  /** * @param urlstring * @return/public string gethtmlstring (string urlstring) {try {URL url
      = NULL;
      url = new URL (urlstring);
      URLConnection Ucon = null;
      Ucon = Url.openconnection ();
      InputStream instr = null;
      InStr = Ucon.getinputstream ();
      Bufferedinputstream bis = new Bufferedinputstream (instr);
      Bytearraybuffer BAF = new Bytearraybuffer (500);
      int current = 0;
      while (current = Bis.read ())!=-1) {Baf.append ((byte) current);
    Return encodingutils.getstring (Baf.tobytearray (), "GBK");
    catch (Exception e) {return ""; }
  }
}

Note code:Elements es = Doc.getelementsbyclass ("Main_navi"); Be sure to find the right position to get the right results. Here is the main preview effect:

For more information on Android-related content readers can view the site topics: "Android Debugging techniques and common problems solution summary", "Android Development introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary

I hope this article will help you with the Android program.

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.