Html代碼查看器,代碼查看器

來源:互聯網
上載者:User

Html代碼查看器,代碼查看器
Html代碼查看器

效果:

 

分析:

1、串連網路需要許可權

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

2、要把預設協議和預設連接埠http和80寫上

3、把訪問網路的代碼放在AsyncTask裡面

4、得到內容類型兩種方法

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

  conn.getContentType

5、分割函數

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

  以等號為分割,取第0段

 

 

代碼:

 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.EditText;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=(EditText) 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         //擷取編輯框輸入的網址37         String urlStr=et_webSite.getText().toString();38         new AsyncTask<String,Void,String>(){39             40             @Override41             protected String doInBackground(String... arg0) {42                 //字串緩衝區43                 StringBuffer buffer= new StringBuffer();44                 try {45                     String s=arg0[0]+":80";46                     //封裝URL對象47                     URL url=new URL(s);48                     //擷取http連線物件49                     HttpURLConnection conn =(HttpURLConnection) url.openConnection();50                     conn.setRequestMethod("GET");51                     conn.setConnectTimeout(5000);52                     //擷取狀態代碼53                     int code=conn.getResponseCode();54                     Log.d("fry", "擷取到狀態代碼:"+code);55                     if(code==200){//請求成功56                         String contentType=conn.getHeaderField("Content-Type");57                         String encoding=contentType.split("=")[0];58                         Log.d("fry", encoding);59                         //擷取響應訊息的實體內容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                             //字元數組轉字串65                             String str=new String(charArr,0,len);66                             //在結尾追加字串67                             buffer.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                 //設定字串到編輯框79                 et_content.setText(result);80                 81             };82             83         }.execute("http://"+urlStr);84     }85     86 }

 

本來getResponseCode做了串連、擷取狀態代碼,返回狀態代碼三件事。

現在conn.connect()把串連這件事做了。

 

5000ms沒有成功串連就拋出異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.