Android_04_html Source File Viewer
MainActivity. java
Package com.itheima.html viewer; import java. io. inputStream; import java.net. httpURLConnection; import java.net. malformedURLException; import java.net. URL; import com.itheima.html viewer. utils. utils; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. textView; public class MainActivity extends Activity {Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {TextView TV = (TextView) findViewById (R. id. TV); TV. setText (String) msg. obj) ;};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View v) {Thread t = new Thread () {@ Overridepublic void run () {String path = http: // 192.168.1.103: 8080/1. html; try {URL url = new URL (path); // get the connection object. At this time, the HttpURLConnection conn = (HttpURLConnection) url has not been established. openConnection (); conn. setRequestMethod (GET); conn. setConnectTimeout (5000); conn. setReadTimeout (5000); // first establish a connection, and then obtain the response code if (conn. getResponseCode () = 200) {// get the input stream returned by the server. The data in the stream is the html source file InputStream is = conn. getInputStream (); // extract text data from the stream String text = Utils. getTextFromStream (is); // send a Message to refresh the ui of the main thread and display the source file Message msg = handler. obtainMessage (); msg. obj = text; handler. sendMessage (msg) ;}} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}; t. start ();}}
Utils. java
Package com.itheima.html viewer. utils; import java. io. byteArrayOutputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; public class Utils {public static String getTextFromStream (InputStream is) {byte [] B = new byte [1024]; int len = 0; // create a byte array output stream, when reading text data from the input stream, synchronously write the data to the array output stream ByteArrayOutputStream bos = new ByteArrayOutputStream (); try {while (len = is. read (B ))! =-1) {bos. write (B, 0, len );} // convert the data in the byte array output stream to a byte array // The default code table of the Android client is UTF-8 // If garbled characters appear in the Android client, the code table of the server and client is inconsistent // you can choose to modify one of them. In this example, If you modify the client, for example, write the following code: // String text = new String (bos. toByteArray (), UTF-8); // the modified text is encoded with UTF-8. Of course, because the default code table of the Android client is UTF-8, // can be explicitly specified here.
// Another method is to modify the server code table and open it directly with EditPlus,
// When saving it separately, you can modify the format of the code table. // String text = new String (bos. toByteArray (), UTF-8); // use toString () Directly or OK // String text = bos. toString (); // note that String text = new String (bos. toByteArray (); return text;} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return null ;}}
Activity_main.xml
Note:Whether it's an image viewer or an html viewer, you must learn how to convert the stream read by http into an image or text!
For images, it uses Bitmap,
For text, it is OK to convert it into a string directly!