安卓http源码查看器详解

1.效果图如下,输入网址就可以看到该网址的源码

2.项目工程文件如右图所示:

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,最后就是要哦添加访问网络的权限,配置文件如下:

 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,简单六步就完成了操作。

 

posted @ 2015-06-30 21:00  阳阳(ysq)  阅读(925)  评论(0编辑  收藏  举报