Android Project---Htmlparse

Source: Internet
Author: User

When parsing the content on the site, there will always be a lot of HTML tags, usually encountered in this data, you can use the HTML

Such as:

Content.settext (html.fromhtml ("

The text information obtained from title.getcontent () is converted to HTML-formatted content, which solves some problems in which special characters appear due to the different text formats.

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

Official Document: Http://developer.android.com/reference/android/text/Html.html

public class-----Html extends Object

Summary

Class Overview

This class processes the HTML strings into displayable styled text.

Nested Classes
Interfacehtml.imagegetter Retrieves images for HTML tags.
Interfacehtml.taghandler was notified when HTML tags was encountered that the parser does isn't 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 Stringescapehtml(charsequence text)Added in API level 16

Returns an HTML escaped representation of the given plain text.

Public static spannedfromhtml(String source)Added in API Level 1

Returns displayable styled text from the provided HTML string. Any tags in the HTML would display as a generic replacement image which your program can then go through and re Place with real images.

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

Public static spannedfromhtml (String source, Html.imagegetter Imagegetter, Html.taghandler taghandler)Added in API Level 1

Returns displayable styled text from the provided HTML string. Any tags in the HTML would 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 StringtoHtml(spanned text)Added in API Level 1

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

However, if you get a lot of HTML tags and pictures in the text, you need to use two methods in the HTML class and the inner class to manipulate the picture.

Here is a text that parses the HTML format, and the returned spanned type can be used directly as the SetText (spanned s) parameter of the TextView or Editext

 Public classHtmlparser {StaticImagegetter Imagegetter =NewImagegetter () {@Override Publicdrawable getdrawable (String source) {drawable drawable=NULL; Try{URL URL=NewURL (source); Drawable= Drawable.createfromstream (Url.openstream (),""); Drawable.setbounds (0,0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());//set the size of a picture}Catch(malformedurlexception e) {//TODO auto-generated Catch block            } Catch(IOException e) {//TODO auto-generated Catch block            }            returndrawable; }    };//Parse Picture     Public StaticSpanned GetString (String html) {//call the method, passing in an HTML-formatted stringspanned s = html.fromhtml (Html, Imagegetter,NULL); returnS//return spanned type    }}

However, if we want to work with images and text in the background, we need to work with Asynctask. Working with HTML text and pictures in the Doinbackground method

/** * To implement a picture display requires a refactoring method using html.fromhtml: public static spanned * fromhtml (String source, Html.ima Gegetterimagegetter, * html.taghandler * taghandler) where Html.imagegetter is an interface, we want to implement this interface in its GETDRAWABL              The Drawable object that returns a picture in the E * (String Source) method is available. */Imagegetter Imagegetter=NewImagegetter () {@Override Publicdrawable getdrawable (String source) {//TODO auto-generated Method StubURL url; Drawable drawable=NULL; Try{URL=NewURL (source); Drawable=Drawable.createfromstream (Url.openstream (),NULL); Drawable.setbounds (0,0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); } Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                    returndrawable;            }            }; Hotnewsinfo title= list.Get(0); Test= Html.fromhtml (Title.getcontent (), Imagegetter,NULL);

This makes it easy to take care of the special characters appearing in the text and the pictures in the. jpg format.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.