jsoup is a Java a HTML parser can parse a a URL address, HTML text content. It provides a very effortless API , through DOM , CSS and similar jQuery operating method to retrieve and manipulate data.
Jsoup official Chinese Address: http://www.open-open.com/jsoup/parse-document-from-string.htm < / p>
On this website you can find some explanation, . jar file to download, < / span> doc description of the document, etc.
jsoup main functions are as follows:
1. from a URL , documents or string parsing HTML ;
2. use DOM or CSS selector to find and remove the data;
3. operable HTML element, attribute, text;
jsoup is based MIT Protocol release can be freely used on commercial projects.
Here is a pure java code examples have been tested:
- public static ; void main (String [] args) throws IOException {
- try {
- Document doc = Jsoup.connect ( "http://passover.blog.51cto.com/" ). get ();
- System.out.println (doc.title ()); < / span>
- Elements eles = doc.select ( "div.artHead" );
- System.out.println (eles.first (). select (" h3 [class = artTitle] " ));
- } catch (IOException ; e) {
- e.printStackTrace ();
- }
- }
Note: Before running will appear when the following error : < span style = "color: # ff0000;"> android.os.NetworkOnMainThreadException This is because the Android4.0 not supported UI < / span> thread to access the network. Fear of thread blocks suspended animation! There are two ways to solve, an increase in the main program is:
/ / see StrictMode Documents
- StrictMode.setThreadPolicy ( new StrictMode.ThreadPolicy.Builder () ;
- . detectDiskReads ()
- . detectDiskWrites ()
- . detectNetwork () / / or. detectAll () for all detectable problems
- . penaltyLog ()
- . build ());
- StrictMode.setVmPolicy ( new StrictMode.VmPolicy.Builder ()
- . detectLeakedSqlLiteObjects ()
- . detectLeakedClosableObjects ()
- . penaltyLog ()
- . penaltyDeath ()
- . build ());
Another thread is to start download task:
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
/ / start thread download tasks
new Thread (downloadRun). start ();
}
/ **
* download thread
* /
Runnable downloadRun = new Runnable () {
@ Override
public void run () {
/ / TODO Auto-generated method stub
updateListView ();
}
};
Error two: 11-05 19:03:36.299: E / AndroidRuntime (20215): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare ()
error method in front Looper.prepare ();
/ / end of the method plus Looper.loop ();
Jsoup following methods are static class can be called directly. Several methods description
Connect () method to obtain a Connection , and then call Connection objects < / span> get () obtained Document object. Then parse Document object
Connection provides some setup method timeout (), url () etc.
Jsoup some kind of explanation:
Document : extends Element . A HTML document. Ie you send a request, Server send your data <span> . The same can also call Element approach. Commonly used methods:
body (), head (), nodeName (), title (), title (String title)
Elements : get HTML the various elements. By Element.select (String str) obtain the corresponding element value
Element first (): return the first matching element, if it is empty, then return null
String text (): return all matching elements mix well text value
Element get (int index);
String attr (String attributeKey);
Element : HTML element by the tag name, attributes, child nodes (including text nodes and other elements) . By the Element, can extract the data, sort node graph, the operation HTML .
text (); return String Type
getElementsByClass (String class);
getAllElements (); returns Elements object
getElementsByAttribute (String key);
Elements select (String selector); based matching selector < / span> select the corresponding Elements
exception details are as follows:
Exception Details : android.view.ViewRoot $ CalledFromWrongThreadException: Only the ; original thread that created a view hierarchy can touch its views.
multi-threaded programming, often outside the main thread needs a separate thread for some processing, and then update the user interface display. However, in the main thread other than the thread directly update the page display problems are: the system will be reported this exception
That must be the program's main thread ( ie < span style = "font-family: Calibri;"> ui thread ) < / span> be updated interface displays job. You can use the following methods to resolve: Can not update the sub-thread UI . To this end, we need Handler object to notify the main thread Ui Thread to update the screen.
- private Handler mHandler = new < / span> Handler () {
- @ Override < / span>
- public void handleMessage (Message msg) {
- switch (msg.what) {
- case UPDATE_UI: {
- Log.i ( "TTSDeamon" , "UPDATE_UI" );
- showTextView.setText (editText.getText (). toString ());
- ShowAnimation ();
- break ;
- }
- default :
- break < / span> ;
- }
- }
- }
demo address 0 Downloads: http://download.csdn.net/detail/zqiang_55/ 4764266
also recommend an application: http://www.talkphone.cn / Down/Soft/Android/Detail/49172_0.html
没有评论:
发表评论