使用Jsoup解析Html == TextView顯示html圖片的方法

Session__csdn發表於2016-06-06

想要做一個看新聞的應用,類似Cnbeta客戶端的東西。大致思路如下:根據連結獲取新聞列表頁的html程式碼,然後解析,找到所有的新聞標題和新聞連結用listView顯示,當點選ListView的Item再載入相應的新聞內容。


其中獲取html程式碼,可以使用如下程式碼實現:

[java] view plain copy
 print?
  1. public String getHtmlString(String urlString) {  
  2.     try {  
  3.         URL url = new URL(urlString);  
  4.         URLConnection ucon = url.openConnection();  
  5.         InputStream instr = ucon.getInputStream();  
  6.         BufferedInputStream bis = new BufferedInputStream(instr);  
  7.         ByteArrayBuffer baf = new ByteArrayBuffer(500);  
  8.         int current = 0;  
  9.         while ((current = bis.read()) != -1) {  
  10.             baf.append((byte) current);  
  11.         }  
  12.         return EncodingUtils.getString(baf.toByteArray(), "gbk");  
  13.     } catch (Exception e) {  
  14.         return "";  
  15.     }  
  16. }  

傳入一個網頁連結,將返回此連結的html程式碼(String)。


然後就是解析此html程式碼了。經過google,發現了java的一個很好用的解析html的庫,Jsoup:http://jsoup.org/

很容易使用,方法類似javascript和JQuery。只需先構建一個Jsoup的Document物件,然後就可以像使用js一個解析html了

[java] view plain copy
 print?
  1. String htmlString = getHtmlString("http://www.cnbeta.com");  
  2. Document document = Jsoup.parse(htmlString);  
比如要獲取cnbeta的html的title,只需:
[java] view plain copy
 print?
  1. String title = document.head().getElementsByTag("title").text();  

另外構建Document的時候也可以直接使用URL,像這樣:

[java] view plain copy
 print?
  1. Document doc = Jsoup.parse(new URL("http://www.cnbeta.com"), 5000);  
其中5000是連線網路的超時時間。


有關Jsoup的下載和更多介紹,見其官網:http://jsoup.org/


我寫的一個demo,點選按鈕後會載入然後顯示cnbeta首頁的所有新聞標題和連結地址,下載:http://download.csdn.net/detail/barryhappy/4151450 ,zip包裡有jsoup的jar包,匯入專案後可能需要手動匯入此jar包。

執行效果圖——

 




===================================================================================


TextView是不只可以String的,我們平常用的給setText()方法傳遞String引數的時候,其實是呼叫的public final void setText (CharSequence text)方法,String類是CharSequence的子類。

而CharSequence子類眾多,其中有一個介面Spanned,即類似html的帶標記的文字。我們可以用它來在TextView中顯示html(自然,有很多html標記是不支援的,只支援一部分)。


Android.text.Html類的一個方法:

[java] view plain copy
 print?
  1. public static Spanned fromHtml (String source)  
可以將html程式碼轉換為Spanned。

[java] view plain copy
 print?
  1. html = "<h1>this is h1</h1>"  
  2.         + "<p>This text is normal</p>"  
  3.         + "<img src='https://www.google.com.hk/intl/zh-CN/images/logo_cn.png' />";  
  4. Spanned sp = Html.fromHtml(html);  
  5. textView.setText(html);  

顯示效果:

可以看出,字型效果基本是顯示出來了,但是圖片沒有顯示。

要實現圖片的顯示需要使用Html.fromHtml的另外一個重構方法:

public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)

其中Html.ImageGetter是一個介面,我們要實現此介面,在它的getDrawable(String source)方法中返回圖片的Drawable物件才可以。

修改後的程式碼:

[java] view plain copy
 print?
  1. Spanned sp = Html.fromHtml(html, new Html.ImageGetter() {  
  2.     @Override  
  3.     public Drawable getDrawable(String source) {  
  4.         InputStream is = null;  
  5.         try {  
  6.             is = (InputStream) new URL(source).getContent();  
  7.             Drawable d = Drawable.createFromStream(is, "src");  
  8.             d.setBounds(00, d.getIntrinsicWidth(),  
  9.                     d.getIntrinsicHeight());  
  10.             is.close();  
  11.             return d;  
  12.         } catch (Exception e) {  
  13.             return null;  
  14.         }  
  15.     }  
  16. }, null);  
  17. textView.setText(sp);  
看起來有些複雜,但其實只是fromHtml()的第二個引數是一個匿名類,用以圖片的獲取。

其中

[java] view plain copy
 print?
  1. is = (InputStream) new URL(source).getContent();  
  2. Drawable d = Drawable.createFromStream(is, "src");  
用以通過圖片的地址獲取相應的Drawable例項。

由於用到了網路資源的圖片,所以要在Mainifest檔案中加入許可權:

[html] view plain copy
 print?
  1. <uses-permission android:name="android.permission.INTERNET" />  
修改後的執行結果:


圖片正常顯示了。


我做的demo,下載地址:http://download.csdn.net/detail/barryhappy/4154342


相關文章