Android 網路HTML查看器

來源:互聯網
上載者:User

標籤:

本文實現一個基於Android的網路HTML查看器

建立項目,項目布局檔案如下:

<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="fill_parent"        android:layout_height="wrap_content"        android:hint="請輸入html路徑" />    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:onClick="click"        android:text="查看" />    <ScrollView        android:layout_width="fill_parent"        android:layout_height="fill_parent" >        <TextView            android:id="@+id/tv_content"            android:layout_width="fill_parent"            android:layout_height="fill_parent" >        </TextView>    </ScrollView></LinearLayout>

完整代碼如下:

package com.wuyudong.htmlviewer;import java.io.InputStream;import java.io.StreamCorruptedException;import java.net.HttpURLConnection;import java.net.URL;import com.wuyudong.htmlviewer.utils.StreamTools;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.text.TextUtils;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 static final int ERROR = 1;    protected static final int SHOW_TEXT = 2;    private TextView tv_content;    private EditText et_path;    // 定義一個訊息處理器    private Handler handler = new Handler() {        public void handleMessage(android.os.Message msg) {            switch (msg.what) {            case ERROR:                Toast.makeText(MainActivity.this, "擷取資料失敗", 0).show();                break;            case SHOW_TEXT:                String text = (String) msg.obj;                tv_content.setText(text);                break;            }        };    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv_content = (TextView) findViewById(R.id.tv_content);        et_path = (EditText) findViewById(R.id.et_path);    }    public void click(View view) {        final String path = et_path.getText().toString().trim();        if (TextUtils.isEmpty(path)) {            Toast.makeText(this, "路徑不可為空", 0).show();        } else {            new Thread() {                public void run() {                    try {                        URL url = new URL(path);                        HttpURLConnection conn = (HttpURLConnection) url                                .openConnection();                        conn.setRequestMethod("GET");                        conn.setConnectTimeout(5000);                        conn.setReadTimeout(5000);                        conn.setRequestProperty(                                "User-Agent",                                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");                        int code = conn.getResponseCode();                        if (code == 200) {                            InputStream is = conn.getInputStream();                            String result = StreamTools.readInputStream(is);                            // tv_content.setText(result);                            Message msg = new Message();                            msg.what = SHOW_TEXT;                            msg.obj = result;                            handler.sendMessage(msg);                        } else {                            Message msg = new Message();                            msg.what = ERROR;                            handler.sendMessage(msg);                        }                    } catch (Exception e) {                        e.printStackTrace();                        Message msg = new Message();                        msg.what = ERROR;                        handler.sendMessage(msg);                    }                };            }.start();        }    }}

 

Android 網路HTML查看器

聯繫我們

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