博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 制作一个网页源代码浏览器(HttpURLConnection)
阅读量:5332 次
发布时间:2019-06-14

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

package com.wuyou.htmlcodeviewer;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.ActionBarActivity;import android.text.TextUtils;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class MainActivity extends ActionBarActivity {    private static final int ERROR = 0;    private static final int CHANGE_TEXT = 1;    private EditText editText;    private TextView textView;    private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            if (msg.what == CHANGE_TEXT) {                textView.setText((String) msg.obj);            } else if (msg.what == ERROR) {                Toast.makeText(MainActivity.this, (String) msg.obj, Toast.LENGTH_LONG).show();            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        editText = (EditText) findViewById(R.id.et);        textView = (TextView) findViewById(R.id.tv);    }    public void click(View view) {        new Thread(new Runnable() {            @Override            public void run() {                try {                    String path = editText.getText().toString().trim();                    if (TextUtils.isEmpty(path)) {                        Message message = new Message();                        message.what = ERROR;                        message.obj = "地址不能为空!";                        handler.sendMessage(message);                    } else {                        URL url = new URL(path);                        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();                        httpURLConnection.setRequestMethod("GET");                        httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");                        httpURLConnection.setConnectTimeout(5000);//设置连接服务器如果没有在5秒钟运行则抛出错误                        if (httpURLConnection.getResponseCode() == 200) {                            InputStream is = httpURLConnection.getInputStream();                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));                            String line = null;                            StringBuilder sb = new StringBuilder();                            while ((line = bufferedReader.readLine()) != null) {                                sb.append(line);                            }                            Message message = new Message();                            message.what = CHANGE_TEXT;                            message.obj = sb.toString();                            handler.sendMessage(message);                        }                    }                } catch (Exception e) {                    e.printStackTrace();                    Message message = new Message();                    message.what = ERROR;                    message.obj = "出错了!";                    handler.sendMessage(message);                }            }        }).start();    }}

 

 

<uses-permission android:name="android.permission.INTERNET"/>

转载于:https://www.cnblogs.com/wuyou/p/3426830.html

你可能感兴趣的文章
c++开源项目汇总
查看>>
python yield返回多个值
查看>>
每日站立会议及今日份任务
查看>>
R12 付款过程请求-功能和技术信息 (文档 ID 1537521.1)
查看>>
洛谷 4364 [九省联考2018]IIIDX
查看>>
洛谷 3870 [TJOI2009]开关
查看>>
【牛客-16643】统计数字(简单排序)
查看>>
www.aaa.com/index.html跳转www.aaa.com设置
查看>>
ssdb binlog机制 存疑
查看>>
Vue 2.0 组件库总结
查看>>
HDU5033 Building(单调栈)
查看>>
Kafka 安装配置 及 简单实验记录
查看>>
想成为程序猿?28个程序员专供在线学习网站(转)
查看>>
font-style: oblique文字斜体,display:inline-block显示间隙
查看>>
css设置滚动条并显示或隐藏
查看>>
【leetcode❤python】13. Roman to Integer
查看>>
常用关于 JavaScript 中的跨域访问方法
查看>>
织梦万能调用LOOP标签!
查看>>
Microsoft 官网 socket异步
查看>>
asp.net MVC helper 和自定义函数@functions小结
查看>>