下载 HTML 网页

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xiesir.example20downhtml"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.xiesir.example20downhtml.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btnDown" android:text="查看" android:onClick="downClick" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/etURL" android:hint="请输入网址" android:lines="1" android:layout_toLeftOf="@id/btnDown" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> <TextView android:id="@+id/tv" android:scrollbars="vertical" android:text="显示网页源码" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
MainActivity.java:
package com.xiesir.example20downhtml; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import javax.net.ssl.HttpsURLConnection; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } Handler handler = new Handler() { public void handleMessage(Message msg) { TextView tv = (TextView) findViewById(R.id.tv); // 让android的TextView可以滚动 // 1. 设置ScrollingMovementMethod // TextView.setMovementMethod(new ScrollingMovementMethod()); // // 2. XML中配置: // android:scrollbars="vertical" tv.setMovementMethod(new ScrollingMovementMethod()); tv.setText((String) msg.obj); } }; public void downClick(View v) { // // Android 4.x版本前可以直接使用以下代码,但Android 4.x版本以后为了保证良好的用户体验, // // 应避免在主线程中包含耗时操作(象网络操作),如果有耗时操作应写在其它线程中。 // String path = "http://www.cnblogs.com/pcideas/category/751613.html"; // // 发送http请求 // try { // // 1.使用网址构造一个URL对象 // URL url = new URL(path); // // 2.获取连接对象 // HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // // 3.设置一些属性 // // 设置请求方式,注意大写 // conn.setRequestMethod("GET"); // // 设置请求超时 // conn.setConnectTimeout(8000); // // 设置读取超时 // conn.setReadTimeout(8000); // // 4.发送请求,建立连接 // conn.connect(); // // 5.判断请求是否成功 // if(conn.getResponseCode() == 200){ // // 获取服务器返回的流,流里包含客户端请求的数据 // InputStream is = conn.getInputStream(); // // // 读取流里的数据 // String text = getTextFromStream(is); // TextView tv = (TextView) findViewById(R.id.tv); // tv.setText(text); // } // else{ // Toast.makeText(this, "请求失败鸟o(╯□╰)o", Toast.LENGTH_LONG).show(); // } // } catch (Exception e) { // e.printStackTrace(); // } EditText etURL = (EditText) findViewById(R.id.etURL); final String path; if (etURL.getText().toString().isEmpty()) path = "http://www.cnblogs.com/pcideas/category/751613.html"; else path = etURL.getText().toString(); Thread t = new Thread() { @Override public void run() { URL url; try { // 1.使用网址构造一个URL对象 url = new URL(path); // 2.获取连接对象 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 3.设置一些属性 // 设置请求方式,注意大写 conn.setRequestMethod("GET"); // 设置请求超时 conn.setConnectTimeout(8000); // 设置读取超时 conn.setReadTimeout(8000); // 4.发送请求,建立连接,获取响应码,判断请求是否成功 if (conn.getResponseCode() == 200) { // 获取服务器返回的流,流里包含客户端请求的数据 InputStream is = conn.getInputStream(); String text = getTextFromStream(is); // 只有主线程才能刷新 UI // 使用Handler让子线程通过Message(消息)刷新 UI // http://www.zhihu.com/question/37334646 // 主线程创建时,同时也会创建 MessageQueue(消息队列)和 Looper(消息轮询器)对象 // 如果需要使用消息队列机制,程序员需要自行创建 Handler(消息处理器) // 轮询器不断检测消息队列中是否有消息,如果有,就会把消息取出,交给Handler对象 // Handler对象拿到消息之后,就会在主线程执行handleMessage() // 也就是说子线程,只要往消息队列中发送消息,主线程立刻执行handleMessage() // 发送消息至消息队列,主线程会执行handleMessage Message msg = handler.obtainMessage(); msg.obj = text; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } } }; t.start(); } public static String getTextFromStream(InputStream is){ byte[] b = new byte[1024]; int len; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { while((len = is.read(b)) != -1){ bos.write(b, 0, len); } // 把字节数组输出流转换成字节数组,然后用字节数组构造一个字符串 String text = new String(bos.toByteArray(), "UTF-8"); return text; } catch (Exception e) { e.printStackTrace(); } return null; } }
参考
浙公网安备 33010602011771号