博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 网络HTML查看器
阅读量:5908 次
发布时间:2019-06-19

本文共 4158 字,大约阅读时间需要 13 分钟。

本文实现一个基于Android的网络HTML查看器

新建项目,项目布局文件如下:

新建工具类StreamTools.java:

package com.wuyudong.htmlviewer.utils;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class StreamTools {    /**     * 把输入流的内容转化成字符串     *      * @param is     * @return     */    public static String readInputStream(InputStream is) {        try {            ByteArrayOutputStream baos = new ByteArrayOutputStream();            int len = 0;            byte[] buffer = new byte[1024];            while ((len = is.read(buffer)) != -1) {                baos.write(buffer, 0, len);            }            is.close();            baos.close();            byte[] result = baos.toByteArray();            return new String(result);        } catch (Exception e) {            e.printStackTrace();            return null;        }    }}

完整代码如下:

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();        }    }}

 

转载地址:http://wpvpx.baihongyu.com/

你可能感兴趣的文章
四则运算
查看>>
Qt5 for Android: incompatible ABI
查看>>
zookeeper学习
查看>>
Sharepoint contant class
查看>>
class类名的管理
查看>>
LeetCode:Rectangle Area
查看>>
文本查询
查看>>
查看帐号授权信息
查看>>
小程序(四):模板
查看>>
Eclipse 常用快捷键
查看>>
【转】Java - printf
查看>>
Xcode 调试方法总结
查看>>
jquery获取元素到屏幕底的可视距离
查看>>
15道简单算法题
查看>>
ENDNOTE使用方法(转发)
查看>>
汉诺塔--递归和非递归实现
查看>>
2019春总结作业
查看>>
Cherokee 1.0.13 发布
查看>>
axis调用webservice的简单方法
查看>>
could not initialize proxy - no Session
查看>>