安卓http源碼查看器詳解

1.效果圖以下,輸入網址就能夠看到該網址的源碼java

2.項目工程文件如右圖所示:android

3.首先,佈局文件以下,我採用的是線性佈局數組

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     >
 7    <EditText 
 8        android:id="@+id/et_info"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="http://" />
12     <Button
13         android:onClick="click"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:layout_gravity="center_horizontal"
17         android:text="查看源碼" />
18     <ScrollView
19          android:layout_width="match_parent"
20          android:layout_height="match_parent"
21          >
22     <TextView
23         android:id="@+id/tv_info"
24         android:layout_width="match_parent"
25         android:layout_height="match_parent"
26         />
27     </ScrollView>
28 
29 </LinearLayout>

4.MainActivity代碼以下所示:採用子線程,由於安卓4.0版本以上不支持主線程。服務器

  1 package com.xunfang.look;
  2 
  3 import java.io.InputStream;
  4 import java.net.HttpURLConnection;
  5 import java.net.MalformedURLException;
  6 import java.net.URL;
  7 import java.net.URLConnection;
  8 
  9 import com.xunfang.service.StreamUtils;
 10 
 11 import android.app.Activity;
 12 import android.os.Bundle;
 13 import android.os.Handler;
 14 import android.os.Message;
 15 import android.text.TextUtils;
 16 import android.view.Menu;
 17 import android.view.MenuItem;
 18 import android.view.View;
 19 import android.widget.EditText;
 20 import android.widget.TextView;
 21 import android.widget.Toast;
 22 
 23 
 24 public class MainActivity extends Activity {
 25     protected static final int SUCCESS = 1;
 26     protected static final int ERROR = 2;
 27     protected static final int FAULED = 3;
 28     private EditText et;
 29     private TextView tv;
 30     
 31         //此方法由主線程調用
 32     private Handler handler = new Handler(){
 33         public void handleMessage(Message msg) {
 34             int type = msg.what;
 35             switch (type) {
 36             case SUCCESS:
 37                 String info = (String) msg.obj;
 38                 tv.setText(info);
 39                 break;
 40             case ERROR:
 41                 //Toast.makeText(getApplicationContext(), "網絡鏈接錯誤,請稍後再試", 0).show();
 42                 String error = (String) msg.obj;
 43                 Toast.makeText(getApplicationContext(), error, 0).show();
 44                 break;
 45             case FAULED:
 46                 String fail = (String) msg.obj;
 47                 Toast.makeText(getApplicationContext(), fail, 0).show();
 48                 break;
 49             default:
 50                 break;
 51             }
 52         };
 53     };
 54     @Override
 55     protected void onCreate(Bundle savedInstanceState) {
 56         super.onCreate(savedInstanceState);
 57         setContentView(R.layout.activity_main);
 58         et = (EditText) findViewById(R.id.et_info);
 59         tv = (TextView) findViewById(R.id.tv_info);
 60         
 61     }
 62     public void click(View view){
 63     new Thread(){
 64         public void run(){
 65             //獲取路徑
 66             String path = et.getText().toString().trim();
 67             
 68             if(TextUtils.isEmpty(path)){
 69                 System.out.println("路徑不能爲空!");
            return ;
70 } 71 try { 72 URL uri = new URL(path); 73 //鏈接到服務器 74 HttpURLConnection http = (HttpURLConnection) uri.openConnection(); 75 //設置響應時間 76 http.setConnectTimeout(5000); 77 //拿到返回的狀態碼 78 int n = http.getResponseCode(); 79 80 if(n == 200){ 81 //接收服務器返回的內容 82 InputStream is = http.getInputStream(); 83 //從流中解析出字符串 84 String info = StreamUtils.readStream(is); 85 //c建立消息 86 Message msg = Message.obtain(); 87 //設置消息內容 88 msg.obj = info; 89 //類型 90 msg.what = SUCCESS; 91 //發送消息 92 handler.sendMessage(msg) ; 93 }else{ 94 Message msg = Message.obtain(); 95 msg.obj = "請求失敗"; 96 msg.what = FAULED; 97 handler.sendMessage(msg); 98 99 } 100 101 } catch (Exception e) { 102 e.printStackTrace(); 103 Message msg = Message.obtain(); 104 msg.obj = "網絡不通,請稍後再試"; 105 msg.what = ERROR; 106 handler.sendMessage(msg) ; 107 } 108 } 109 }.start(); 110 } 111 112 }

5.IO流,從給定的流中讀取全部的數據轉成字符串,由於要接收服務器的數據網絡

 1 package com.xunfang.service;
 2 
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 
 7 public class StreamUtils {
 8     //從給定的流中讀取全部的數據轉成字符串
 9     public static String readStream(InputStream is) {
10             try {
11                 byte[] bs = new byte[1024];
12                 int b = 0;
13                 //捕獲內存緩衝區的數據,轉換成字節數組
14                 ByteArrayOutputStream boas = new ByteArrayOutputStream();
15                 
16                 while((b=is.read(bs))!= -1){
17                     /* write(byte[] b, int off, int len),
18                     將指定 byte數組中從偏移量 off 開始的 len 個字節寫入此 byte 數組輸出流。
19                      b - 數據。
20                     off - 數據的初始偏移量。
21                     len - 要寫入的字節數。 */
22                     boas.write(bs, 0, b);
23                 }
24                 boas.close();
25                 return new String(bs);
26                 
27             } catch (Exception e) {
28                 e.printStackTrace();
29             }
30         
31         return null;
32     }
33 }

6,最後就是要哦添加訪問網絡的權限,配置文件以下:app

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.xunfang.look"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="16"
 9         android:targetSdkVersion="16" />
10     <uses-permission android:name="android.permission.INTERNET"/>
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name=".MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21                 <category android:name="android.intent.category.LAUNCHER" />
22             </intent-filter>
23         </activity>
24     </application>
25 
26 </manifest>

7,簡單六步就完成了操做。ide

相關文章
相關標籤/搜索