Android Learning (*)--HTML source file Viewer

Source: Internet
Author: User
Tags response code


send a GET request
URL url = new URL (path);//Get Connection object HttpURLConnection conn = (httpurlconnection) url.openconnection ();// Set connection Properties Conn.setrequestmethod ("GET"), Conn.setconnecttimeout (Conn.setreadtimeout (5000);//Establish connection, get response if ( Conn.getresponsecode () = = 200) {}

gets the stream returned by the server and reads the HTML source from the stream Because of the build string, it can be built with new string (byte[]), and Bytearrayoutputstream can call. Tobytearray (), Ability to convert a stream directly to a byte array
Byte[] B = new Byte[1024];int len = 0; Bytearrayoutputstream BOS = new Bytearrayoutputstream (), while (len = Is.read (b))! =-1) {    //writes bytes read first to byte array output stream to save    Bos.write (b, 0, Len);} Converts the contents of the byte array output stream to a string//by default using Utf-8text = new String (Bos.tobytearray ());

Handling of garbled charactersgarbled appearance due to inconsistent server and client code table
Manually specify the code table Text = new String (Bos.tobytearray (), "utf-8");

core code
public class Mainactivity extends Activity {Handler Handler = new Handler () {public void Handlemessage (android.os.Message m SG) {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/bai Du.html "; try {URL url = new URL (path);//Get Connection object, no connection is established at this time httpurlconnection conn = (httpurlconnection) url.openconnection ( ); Conn.setrequestmethod ("GET"); Conn.setreadtimeout (5000);//Establish the connection first, 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 the HTML InputStream is = Conn.getinputstream ();// Take the text data out of the stream. String text = Utils.gettextfromstream (is);//Send message, let the main thread refresh UI, display source file message msg = Handler.obtainmessage (); Msg.obj = Text;handler.sendmessage (msg);}} catch (Exception e) {//TODO auto-generated catch BLOCKE.PRintstacktrace ();}}; T.start ();}}

Get the string from the stream
public class Utils {public static String Gettextfromstream (InputStream is) {byte[] b = new Byte[1024];int len = 0;//Create byte array output Out stream, when reading the text data of the input stream, synchronously writes the data to the array output stream bytearrayoutputstream Bos = new Bytearrayoutputstream (); try {while (len = Is.read (b))! =-1) {Bos.write (b, 0, Len);} Converts the data in the byte array output stream into a byte array, string text = new String (Bos.tobytearray ()); return text;} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;}}



Android Learning (*)--HTML source file Viewer

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.