Html code viewer, code Viewer

Source: Internet
Author: User
Tags array to string

Html code viewer, code Viewer
Html code Viewer

Effect:

 

Analysis:

1. permissions required to connect to the network

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

2. Write the default protocol and default ports http and 80

3. Put the network access code in AsyncTask.

4. Two methods for obtaining content types

String contentType = conn. getHeaderField ("Content-Type ");

Conn. getContentType

5. Split Functions

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

Divide by equal signs, and take 0th segments

 

 

Code:

1 package fry; 2 3 import java. io. inputStreamReader; 4 import java.net. httpURLConnection; 5 import java.net. malformedURLException; 6 import java.net. URL; 7 8 import com. example. watchCode. r; 9 10 import android. app. activity; 11 import android. OS. asyncTask; 12 import android. OS. bundle; 13 import android. util. log; 14 import android. view. view; 15 import android. widget. button; 16 import android. widget. editTex T; 17 18 public class Activity01 extends Activity {19 private Button btn_checked; 20 private EditText et_webSite; 21 private EditText et_content; 22 @ Override23 protected void onCreate (Bundle savedInstanceState) {24 // TODO Auto-generated method stub25 super. onCreate (savedInstanceState); 26 setContentView (R. layout. activity01); 27 28 btn_checked = (Button) findViewById (R. id. btn_checked); 29 et_webSite = (EditT Ext) findViewById (R. id. et_webSite); 30 et_content = (EditText) findViewById (R. id. et_content); 31 32} 33 34 35 public void onClick (View view) {36 // obtain the URL 37 String urlStr = et_webSite.getText () entered in the edit box (). toString (); 38 new AsyncTask <String, Void, String> () {39 40 @ Override41 protected String doInBackground (String... arg0) {42 // String buffer 43 StringBuffer buffer = new StringBuffer (); 44 try {45 String s = arg0 [0] + ": 80"; 46 // Encapsulate URL object 47 URL url = new URL (s); 48 // GET http connection object 49 HttpURLConnection conn = (HttpURLConnection) url. openConnection (); 50 conn. setRequestMethod ("GET"); 51 conn. setConnectTimeout (5000); 52 // get status code 53 int code = conn. getResponseCode (); 54 Log. d ("fry", "Get Status code:" + code); 55 if (code = 200) {// request successful 56 String contentType = conn. getHeaderField ("Content-Type"); 57 String encoding = contentType. split ("=") [0]; 58 Log. d ("fry", encod Ing); 59 // obtain the object content of the Response Message 60 InputStreamReader reader = new InputStreamReader (conn. getInputStream (), "UTF-8"); 61 char [] charArr = new char [1024*8]; 62 int len = 0; 63 while (len = reader. read (charArr ))! =-1) {64 // character array to String 65 String str = new String (charArr, 0, len); 66 // append String 67 buffer at the end. append (str); 68} 69} 70 71} catch (Exception e) {72 e. printStackTrace (); 73} 74 75 return buffer. toString (); 76} 77 protected void onPostExecute (String result) {78 // set the String to the edit box 79 et_content.setText (result); 80 81 }; 82 83. cmd.exe cute ("http: //" + urlStr); 84} 85 86}

 

Originally, getResponseCode connected and obtained status codes, and returned status codes.

Now conn. connect () Does the connection.

 

If the connection fails for MS, an exception is thrown.

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.