2013年8月16日星期五

Android_HTML parser _jsoup

 
  

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:

  

  
   
    
      [java]       view plain       copy     
   
   
        
  1. public static ; void main (String [] args) throws IOException {
  2.     
  3. try {
  4.     
  5. Document doc = Jsoup.connect ( "http://passover.blog.51cto.com/" ). get ();
  6.     
  7. System.out.println (doc.title ()); < / span>
  8.     
  9. Elements eles = doc.select ( "div.artHead" );
  10.     
  11. System.out.println (eles.first (). select (" h3 [class = artTitle] " ));
  12.     
  13. } catch (IOException ; e) {
  14.     
  15. e.printStackTrace ();
  16.     
  17. }
  18.     
  19. }
  20.    
  
  

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

  
   
    
      [java]       view plain       copy     
   
   
        
  1. StrictMode.setThreadPolicy ( new StrictMode.ThreadPolicy.Builder () ;
  2.     
  3. . detectDiskReads ()
  4.     
  5. . detectDiskWrites ()
  6.     
  7. . detectNetwork () / / or. detectAll () for all detectable problems
  8.     
  9. . penaltyLog ()
  10.     
  11. . build ());
  12.     
  13. StrictMode.setVmPolicy ( new StrictMode.VmPolicy.Builder ()
  14.     
  15. . detectLeakedSqlLiteObjects ()
  16.     
  17. . detectLeakedClosableObjects ()
  18.     
  19. . penaltyLog ()
  20.     
  21. . penaltyDeath ()
  22.     
  23. . build ());
  24.    
  
  

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.

  
   
    
      [java]       view plain       copy     
   
   
        
  1. private Handler mHandler = new < / span> Handler () {
  2.     
  3. @ Override < / span>
  4.     
  5. public void handleMessage (Message msg) {
  6.     
  7. switch (msg.what) {
  8.     
  9. case UPDATE_UI: {
  10.     
  11. Log.i ( "TTSDeamon" , "UPDATE_UI" );
  12.     
  13. showTextView.setText (editText.getText (). toString ());
  14.     
  15. ShowAnimation ();
  16.     
  17. break ;
  18.     
  19. }
  20.     
  21. default :
  22.     
  23. break < / span> ;
  24.     
  25. }
  26.     
  27. }
  28.     
  29. }
  30.    
  
  

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

 

没有评论:

发表评论