一。权限
1 2 3 |
<uses-permission android:name="android.permission.INTERNET"/> |
二。界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!-- 填充屏幕,垂直排列 --> <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" > <!-- 宽:占满全局,高:适应自身,排列:水平 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <!-- URL文本框。ID:新建id,宽:比例像素,单行:是,权重:占满水平闲置空间 --> <EditText android:id="@+id/et_url" android:layout_width="0dip" android:text="http://www.gaohaiyan.com" android:layout_height="wrap_content" android:singleLine="true" android:layout_weight="1" /> <!-- 按钮。点击事件:getHtml --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="getHtml" android:text="GO" /> </LinearLayout> <!-- 滚动框 --> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 文本框:显示网页源码 --> <TextView android:id="@+id/tv_html" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </ScrollView> </LinearLayout> |
三。主程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
public class MainActivity extends ActionBarActivity { private static final String TAG = "MainActivity"; private static final int SUCCESS = 0; protected static final int ERROR = 1; private EditText etUrl; private TextView tvHtml; /** * 1.消息处理器 */ private Handler handler = new Handler() { /** * 3.接收消息 */ public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SUCCESS: tvHtml.setText((String) msg.obj); break; case ERROR: Toast.makeText(MainActivity.this, "访问失败", 0).show(); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etUrl = (EditText) findViewById(R.id.et_url); tvHtml = (TextView) findViewById(R.id.tv_html); } /** * 处理按钮被点击事件 */ public void getHtml(View v) { // 获取文本框内的URL final String url = etUrl.getText().toString(); /** * 子线程处理 */ new Thread(new Runnable() { public void run() { // 请求网络 String html = getHtmlFromInternet(url); if(!TextUtils.isEmpty(html)) { // 更新textview的显示了 Message msg = new Message(); msg.what = SUCCESS; msg.obj = html; /** * 2.向消息处理器发送消息 */ handler.sendMessage(msg); } else { Message msg = new Message(); msg.what = ERROR; handler.sendMessage(msg); } } }).start(); } /** * 获取html代码 */ protected String getHtmlFromInternet(String url) { try { URL mURL = new URL(url); HttpURLConnection conn = (HttpURLConnection) mURL.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); int responseCode = conn.getResponseCode(); if(responseCode == 200) { // 得到服务器的响应状态码 InputStream is = conn.getInputStream(); // 获得服务器返回的流数据 // 处理数据 String html = getStringFromInputStream(is); return html; } else { Log.i(TAG, "访问失败: " + responseCode); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 根据流返回一个字符串信息 */ private String getStringFromInputStream(InputStream is) throws IOException { // 捕获内存缓冲区的数据,转换成字节数组 ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } is.close(); String html = baos.toString(); // 把流中的数据转换成字符串, 默认采用安卓的编码 utf-8 // 如果页面包含gbk, gb2312编码, 就采用gbk编码进行对字符串编码 if(html.contains("gbk") || html.contains("gb2312") || html.contains("GBK") || html.contains("GB2312")) { // 重新编码 html = new String(baos.toByteArray(), "gbk"); } baos.close(); return html; } } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/169.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设