Android development tool Class 24_gethtml

Source: Internet
Author: User

Get Web page (JSP) source code

1 ImportJava.io.InputStream;2 Importjava.net.HttpURLConnection;3 ImportJava.net.URL;4 5 ImportCom.wangjialin.internet.utils.StreamTool;6 7  Public classHtmlservice {8     /**9 * Access to Web sourceTen      * @paramPath Web Paths One      * @return A      */ -      Public StaticString gethtml (String path)throwsException { -          theHttpURLConnection conn = (httpurlconnection)NewURL (path). OpenConnection (); -Conn.setconnecttimeout (5000); -Conn.setrequestmethod ("GET"); -          +         if(Conn.getresponsecode () = = 200){ -              +InputStream instream =Conn.getinputstream (); A             byte[] data =Streamtool.read (instream); at             return NewString (data); -         } -         return NULL; -     } -}

Streamtool.java

1 ImportJava.io.ByteArrayOutputStream;2 ImportJava.io.InputStream;3 4  Public classStreamtool {5 6     /**7 * Read data from the stream8      * @paraminstream9      * @returnTen      */ One      Public Static byte[] Read (InputStream instream)throwsexception{ A          -Bytearrayoutputstream OutputStream =NewBytearrayoutputstream (); -         byte[] buffer =New byte[1024]; the         intLen = 0; -          -          while(len = instream.read (buffer))! =-1){ -Outputstream.write (buffer, 0, Len); +         } - instream.close (); +         returnOutputstream.tobytearray (); A     } at}

Show Web page Source Pathtext = "http://192.168.1.103:8080/ServerForWebCodeViewer/wangjialin.jsp"

1      Public voidshowhtml (View v) {2         3Path =Pathtext.gettext (). toString ();4         Try {5             NewThread (NewRunnable () {6 7 @Override8                  Public voidrun () {9                     //TODO auto-generated Method StubTen                     Try { OneHTML =htmlservice.gethtml (path); A}Catch(Exception e) { -                         //TODO auto-generated Catch block - e.printstacktrace (); the                     } -                 }                 - }). Start (); -              + Textview.settext (HTML); -}Catch(Exception e) { + e.printstacktrace (); A Toast.maketext (Getapplicationcontext (), R.string.error, Toast.length_long). Show (); at         } -  -}

Android development tool Class 24_gethtml

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.