In the previous articles, we introduced the use of common Jsoup classes: jsoup Document class, Jsoup class, jsoup Node class, jsoup Element class, And jsoup Elements class. These are the translations of common APIs. Here is a simple example:
MainActivity:
Package com. home. testjsoup; import java. io. IOException; import java.net. malformedURLException; import java.net. URL; 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. button; public class MainActiv Ity extends Activity implements OnClickListener {private Button btn; private static final String URL_STR = "http://vip.astro.sina.com.cn/iframe/astro/view/aries/day/"; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); btn = (Button) findViewById (R. id. main_btn); btn. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {If (v = btn) {new Thread (r ). start () ;}} Runnable r = new Runnable () {Document doc; @ Overridepublic void run () {try {doc = Jsoup. parse (new URL (URL_STR), 10000);} catch (MalformedURLException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} if (doc! = Null) {doParse (doc) ;}};/*** parse some html content ** @ param doc */private void doParse (Document doc) {// parse the constellation description String describe = doc. getElementsByClass ("lotconts "). first (). text (); System. out. println (describe); // parse the valid date String validDate = doc. getElementsByClass ("datea "). first (). text (); System. out. println (validDate); // parse the constellation name and date String constellationDate = doc. select ("span "). first (). text (); System. out. printl N (constellationDate); // parse all the Elements whose class is "tab" elements = doc. getElementsByClass ("tab"); if (elements! = Null) {for (Element e: elements) {// module title String title = e. select ("h4 "). get (0 ). text (); System. out. println (title); // The text content between p tags. If not, it is nullString pText = e. select ("p "). get (0 ). text (); System. out. println (pText); // src link address Elements linkText = e. select ("[src]"); System. out. println (linkText. attr ("src "));}}}}