Page:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "android:orientation= "vertical" > <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "View Source file"Android:onclick= "click" /> <ScrollViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" > <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></ScrollView></LinearLayout>
Java:
PackageCom.itheima.htmlviewer;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;Importcom.itheima.htmlviewer.utils.Utils;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.TextView;//request text on-line (Web page) Public classMainactivityextendsActivity {Handler Handler=NewHandler () { Public voidhandlemessage (android.os.Message msg) {TextView TV=(TextView) Findviewbyid (r.id.tv); Tv.settext ((String) msg.obj); } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Public voidClick (View v) {Thread T=NewThread () {@Override Public voidrun () {String path= "Http://192.168.13.13:8080/baidu.html"; Try{URL URL=NewURL (path); //get Connection object, no connection established at this timeHttpURLConnection conn =(HttpURLConnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (5000); Conn.setreadtimeout (5000); //first establish the connection and then get the response code if(Conn.getresponsecode () = = 200){ //get the input stream returned by the server, the data in the stream is the source file of HTMLInputStream is =Conn.getinputstream (); //Take the text data out of the streamString Text =Utils.gettextfromstream (IS); //send a message, let the main thread refresh the UI, display the source fileMessage msg =Handler.obtainmessage (); Msg.obj=text; Handler.sendmessage (msg); } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }; T.start (); }}
Packagecom.itheima.htmlviewer.utils;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classUtils { Public StaticString Gettextfromstream (InputStream is) {//garbled because the server and the client code table are inconsistent byte[] B =New byte[1024]; intLen = 0; //creates a byte array output stream and reads the text data of the input stream, synchronously writes the data to the array output streamBytearrayoutputstream BOS =NewBytearrayoutputstream (); Try { while(len = Is.read (b))! =-1) {Bos.write (b,0, Len);//write to array output stream } //converts data in a byte array output stream into a byte arrayString Text =NewString (Bos.tobytearray (), "utf-8");//Android default is Utf-8 returntext; } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } return NULL; }}
Android 74 Download text