Android_04_html Source File Viewer

Source: Internet
Author: User
Tags response code

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!

 

Related Article

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.