1. 程式人生 > >android篇-如何做一個簡單的安卓源碼查看器

android篇-如何做一個簡單的安卓源碼查看器

android

1,網頁源碼查看器:

Httpurlconnection:用於發送或接收數據

Mainactivity篇:

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;


import android.os.Bundle;

import android.os.Handler;

import android.os.Looper;

import android.os.Message;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;


public class MainActivity extends Activity {


protected final int REQUESTSUCESS = 0; //ctrl + shift + X Y

protected final int REQUESTNOTFOUND = 1;

protected final int REQUESTEXCEPTION = 2;

private EditText et_path;

private TextView tv_reuslt;


//在主線程中定義一個handler

private Handler handler = new Handler(){

//這個方法是在主線程裏面執行的

public void handleMessage(android.os.Message msg) {

//所以就可以在主線程裏面更新ui了

//[1]區分一下發送的是哪條消息

switch (msg.what) {

case REQUESTSUCESS: //代表請求成功

String content = (String) msg.obj;

tv_reuslt.setText(content);

break;

case REQUESTNOTFOUND: //代表請求成功

Toast.makeText(getApplicationContext(), "請求資源不存在", 0).show();

break;

case REQUESTEXCEPTION: //代表請求成功

Toast.makeText(getApplicationContext(), "服務器忙 請稍後....", 1).show();

break;

default:

break;

}

};

};


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// [1]找到我們關心的控件

et_path = (EditText) findViewById(R.id.et_path);

tv_reuslt = (TextView) findViewById(R.id.tv_result);

//[1.1]打印當前線程的名字

System.out.println("當前線程名字:"+Thread.currentThread().getName());

}


//[2]點擊按鈕進行查看 指定路徑的源碼

public void click(View v) {

//[2.0]創建一個子線程

new Thread(){public void run() {

try {

//[2.1]獲取源碼路徑

String path = et_path.getText().toString().trim();

//[2.2]創建URL 對象指定我們要訪問的 網址(路徑)

URL url = new URL(path);

//[2.3]拿到httpurlconnection對象 用於發送或者接收數據

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

//[2.4]設置發送get請求

conn.setRequestMethod("GET");//get要求大寫 默認就是get請求

//[2.5]設置請求超時時間

conn.setConnectTimeout(5000);

//[2.6]獲取服務器返回的狀態碼

int code = conn.getResponseCode();

//[2.7]如果code == 200 說明請求成功

if (code == 200) {

//[2.8]獲取服務器返回的數據 是以流的形式返回的 由於把流轉換成字符串是一個非常常見的操作 所以我抽出一個工具類(utils)

InputStream in = conn.getInputStream();

//[2.9]使用我們定義的工具類 把in轉換成String

String content = StreamTools.readStream(in);

//2.9.0 創建message對象

Message msg = new Message();

msg.what = REQUESTSUCESS;

msg.obj = content;

//2.9.1 拿著我們創建的handler(助手) 告訴系統 說我要更新ui

handler.sendMessage(msg); //發了一條消息 消息(msg)裏把數據放到了msg裏 handleMessage方法就會執行

//[2.9.1]把流裏面的數據展示到textview 上 這句話就屬於更新ui的邏輯

// tv_reuslt.setText(content);

}else{

//請求資源不存在 Toast是一個view 也不能在在線程更新ui

Message msg = new Message();

msg.what = REQUESTNOTFOUND;//代表哪條消息

handler.sendMessage(msg);

}

} catch (Exception e) {

e.printStackTrace();

Message msg = new Message();

msg.what = REQUESTEXCEPTION;//代表哪條消息

handler.sendMessage(msg); //發送消息

}

};}.start();

}

}





















//StreamTools與mainactivity放同一個包裏

import java.io.ByteArrayOutputStream;

import java.io.InputStream;


public class StreamTools {


//把一個inputStream 轉換成一個String

public static String readStream(InputStream in) throws Exception{

//定義一個內存輸出流

ByteArrayOutputStream baos = new ByteArrayOutputStream();

int len = -1;

byte[] buffer = new byte[1024]; //1kb

while((len=in.read(buffer))!=-1){

baos.write(buffer, 0, len);

}

in.close();

String content = new String(baos.toByteArray());

return content;

}

}






activity_main篇:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity" >


<EditText

android:id="@+id/et_path"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="請輸入查看網址" />


<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="click"

android:text="查看" />


<ScrollView

android:layout_width="match_parent"

android:layout_height="wrap_content" >


<TextView

android:id="@+id/tv_result"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="hagha" />

</ScrollView>


</LinearLayout>



最後最重要的是勿忘配置許可證

android篇-如何做一個簡單的安卓源碼查看器