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

來源:互聯網
上載者:User

標籤: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篇-如何做一個簡單的安卓源碼查看器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.