HTML Code Viewer

Source: Internet
Author: User
Tags array to string

HTML Code Viewer

Effect:

Analysis:

1. Need permission to connect network

<uses-permission android:name= "Android.permission.INTERNET"/>

2, to the default protocol and default port HTTP and 80 write on

3, put the network access code in the Asynctask inside

4. Two methods of getting content type

String Contenttype=conn.getheaderfield ("Content-type");

Conn.getcontenttype

5. Split function

String encoding=contenttype.split ("=") [0];

Divide by equal sign, take the NO. 0 paragraph

Code:

1  PackageFry;2 3 ImportJava.io.InputStreamReader;4 Importjava.net.HttpURLConnection;5 Importjava.net.MalformedURLException;6 ImportJava.net.URL;7 8 ImportCOM.EXAMPLE.WATCHCODE.R;9 Ten Importandroid.app.Activity; One ImportAndroid.os.AsyncTask; A ImportAndroid.os.Bundle; - ImportAndroid.util.Log; - ImportAndroid.view.View; the ImportAndroid.widget.Button; - ImportAndroid.widget.EditText; -  -  Public classActivity01extendsactivity{ +     PrivateButton btn_checked; -     PrivateEditText Et_website; +     PrivateEditText et_content; A @Override at     protected voidonCreate (Bundle savedinstancestate) { -         //TODO auto-generated Method Stub -         Super. OnCreate (savedinstancestate); - Setcontentview (R.LAYOUT.ACTIVITY01); -          -Btn_checked=(Button) Findviewbyid (r.id.btn_checked); inEt_website=(EditText) Findviewbyid (r.id.et_website); -Et_content=(EditText) Findviewbyid (r.id.et_content); to          +     } -      the      *      Public voidOnClick (view view) { $         //gets the URL entered by the edit boxPanax NotoginsengString urlstr=Et_website.gettext (). toString (); -         NewAsynctask<string,void,string>(){ the              + @Override A             protectedstring Doinbackground (String ... arg0) { the                 //string Buffers +StringBuffer buffer=NewStringBuffer (); -                 Try { $String s=arg0[0]+ ": 80"; $                     //encapsulates a URL object -URL url=NewURL (s); -                     //Get HTTP Connection object theHttpURLConnection conn =(HttpURLConnection) url.openconnection (); -Conn.setrequestmethod ("GET");WuyiConn.setconnecttimeout (5000); the                     //Get Status Code -                     intCode=Conn.getresponsecode (); WuLOG.D ("Fry", "Get to Status code:" +code); -                     if(code==200) {//Request succeeded AboutString Contenttype=conn.getheaderfield ("Content-type"); $String encoding=contenttype.split ("=") [0]; -LOG.D ("Fry", encoding); -                         //get the entity contents of a response message -InputStreamReader reader=NewInputStreamReader (Conn.getinputstream (), "UTF-8"); A                         Char[] chararr=New Char[1024*8]; +                         intLen=0; the                          while((Len=reader.read (Chararr))!=-1){ -                             //character array to string $String str=NewString (chararr,0, Len); the                             //append a string at the end the buffer.append (str); the                         } the                     } -                      in}Catch(Exception e) { the e.printstacktrace (); the                 } About                  the                 returnbuffer.tostring (); the             } the             protected voidOnPostExecute (String result) { +                 //set string to edit box - Et_content.settext (result); the                 Bayi             }; the              the}.execute ("http//" +urlstr); -     } -      the}

Originally Getresponsecode did connect, get status code, return status code three things.

Now Conn.connect () has made the connection to this matter.

5000ms throws an exception without a successful connection

HTML Code 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.