Recently, textview is used to display HTML information on the server. Android provides the HTML. fromhtml () method, which is easy to use.
1. Get the information returned by the server.
2. For example, the server returns
<HTML>
Note that the label must be added with a complete IP address. Otherwise, he cannot read the resource. He has been here for a while, shit.
3. Powerful html. fromhtml ()
Fromhtml (string source, imagegetter,
Taghandler) returns displayable styled text from the provided HTML string. any 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 ). the comments are clear. Download images in imagegetter's getdrawable (asynchronous task download is recommended, but not in demo. If you need it, please leave a message. Welcome to my blog tracyzhang)
4. Check the demo source code:
Package CC. tracyzhang; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. client. clientprotocolexception; import Org. apache. HTTP. client. httpclient; import Org. apache. HTTP. client. methods. httpget; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. XML. sax. xmlreader; imp ORT android. app. activity; import android. graphics. drawable. bitmapdrawable; import android. graphics. drawable. drawable; import android. OS. bundle; import android. text. editable; import android. text. HTML; import android. text. HTML. imagegetter; import android. text. HTML. taghandler; import android. text. method. linkmovementmethod; import android. widget. textview; public class htmlshowdemoactivity extends activity {/** Called when the activity is first created. */textview TV; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); TV = (textview) findviewbyid (R. id. TV); stringbuffer html = new stringbuffer (); httpget get = new httpget ("http://www.baidu.com/"); // httpget get = new httpget ("http: // 192.168.1.102: 8080/htmldemo/index.htm "); httpcli Ent client = new defaulthttpclient (); try {httpresponse response = client.exe cute (get); If (response. getstatusline (). getstatuscode () = 200) {inputstream is = response. getentity (). getcontent (); string S = ""; bufferedreader ISR = new bufferedreader (New inputstreamreader (is); While (S = ISR. readline ())! = NULL) {HTML. append (s);} is. close () ;}} catch (clientprotocolexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} myimagegetter getter = new myimagegetter (); charsequence = html. fromhtml (HTML. tostring (), Getter, null); TV. settext (charsequence); // This statement is indispensable; otherwise, clicking the link will not open the webpage TV. setmovementmethod (linkmovementmethod. getinstance ();} class mytaghandler implements taghandler {@ overridepublic void handletag (Boolean opening, string tag, editable output, xmlreader) {}} class myimagegetter implements imagegetter {@ overridepublic drawable getdrawable (string source) {drawable = NULL; httpget get = new httpget (source); httpclient client = new terminal (); try {httpresponse response = client.exe cute (get); If (response. getstatusline (). getstatuscode () = 200) {inputstream is = response. getentity (). getcontent (); drawable = bitmapdrawable. createfromstream (is, ""); drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight (); is. close () ;}} catch (clientprotocolexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}return drawable ;}}}