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