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 (); }
}