Android項目---HtmlParse

來源:互聯網
上載者:User

標籤:android   des   style   blog   class   code   

在解析網站上的內容的時候,總會出現很多html的標籤,一般在遇到這種資料的時候,就可以用上Html

如:

content.setText(Html.fromHtml("<html><body>" + title.getContent()+ "</body></html>", null, null));

將title.getcontent()擷取的文本資訊轉為html格式的內容,這樣,在一定程度上解決了一些由於文字格式設定的不同而出現特殊字元的問題。

-------------------------------------------------------------------------------------------------------------------------

 

官方文檔:http://developer.android.com/reference/android/text/Html.html

公用類-----Html extends Object

Summary

Class Overview

This class processes HTML strings into displayable styled text. Not all HTML tags are supported.

Nested Classes
interfaceHtml.ImageGetterRetrieves images for HTML <img> tags.
interfaceHtml.TagHandlerIs notified when HTML tags are encountered that the parser does not know how to interpret.
Public Methods
static StringescapeHtml(CharSequence text)
Returns an HTML escaped representation of the given plain text.
static SpannedfromHtml(String source)
Returns displayable styled text from the provided HTML string.
static SpannedfromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
Returns displayable styled text from the provided HTML string.
static StringtoHtml(Spanned text)
Returns an HTML representation of the provided Spanned text.

Public Methodspublic static String escapeHtml (CharSequence text)Added in API level 16

Returns an HTML escaped representation of the given plain text.

public static Spanned fromHtml (String source)Added in API level 1

Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.

This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.

public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)Added in API level 1

Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will use the specified ImageGetter to request a representation of the image (use null if you don‘t want this) and the specified TagHandler to handle unknown tags (specify null if you don‘t want this).

This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.

public static String toHtml (Spanned text)Added in API level 1

Returns an HTML representation of the provided Spanned text.
-------------------------------------------------------------------------------------------------------------------------

但是,如果擷取的文本中有很多的html標籤以及圖片的話,就要用到html類中的兩個方法和處理圖片的內部類

這裡提供一個解析html格式的圖文文本,返回的spanned類型可直接作為textView或者editext的settext(spanned s)參數

public class HtmlParser {    static ImageGetter imageGetter = new ImageGetter() {        @Override        public Drawable getDrawable(String source) {            Drawable drawable = null;            try {                URL url = new URL(source);                drawable = Drawable.createFromStream(url.openStream(), "");                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),                        drawable.getIntrinsicHeight());// 設定圖片的大小            } catch (MalformedURLException e) {                // TODO Auto-generated catch block            } catch (IOException e) {                // TODO Auto-generated catch block            }            return drawable;        }    };// 解析圖片    public static Spanned GetString(String html) {// 調用該方法,傳入html格式的字串        Spanned s = Html.fromHtml(html, imageGetter, null);        return s;//返回spanned類型    }}

但是,又如果,我們想將處理圖片和文本的方法放到後台進行,那就要和AsyncTask進行配合使用了。在doInBackground方法中進行處理html文本和圖片

/**             * 要實現圖片的顯示需要使用Html.fromHtml的一個重構方法:public static Spanned             * fromHtml (String source, Html.ImageGetterimageGetter,             * Html.TagHandler             * tagHandler)其中Html.ImageGetter是一個介面,我們要實現此介面,在它的getDrawable              * (String source)方法中返回圖片的Drawable對象才可以。              */            ImageGetter imageGetter = new ImageGetter() {                @Override                public Drawable getDrawable(String source) {                    // TODO Auto-generated method stub                    URL url;                    Drawable drawable = null;                    try {                        url = new URL(source);                        drawable = Drawable.createFromStream(url.openStream(),                                null);                        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),                                drawable.getIntrinsicHeight());                    } catch (MalformedURLException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    } catch (IOException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                    return drawable;                }            };            HotNewsInfo title = list.get(0);            test = Html.fromHtml(title.getContent(), imageGetter, null);

這樣就能輕鬆搞定,文本中出現的特殊字元和.jpg格式的圖片了。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.