1、编写activity_main.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<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" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查看" /> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </ScrollView> </LinearLayout>
2、编写MainActivity.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.hyzhou.htmlviver; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText edt; private TextView tView; private Handler handler=new Handler() { public void handleMessage(android.os.Message msg) { if (msg.what==1) { String content=(String) msg.obj; tView.setText(content); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edt=(EditText) findViewById(R.id.et); tView=(TextView) findViewById(R.id.tv); } public void click(View view) { final String path=edt.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.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); int code=conn.getResponseCode(); if (code==200) { InputStream in=conn.getInputStream(); String str=StreamRead.readData(in); Message msg=new Message(); msg.what=1; msg.obj=str; handler.sendMessage(msg); }else { Toast.makeText(MainActivity.this, "显示异常", 0).show(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); } } }
3、编写读取html数据类StreamRead.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * */ package com.hyzhou.htmlviver; import java.io.ByteArrayOutputStream; import java.io.InputStream; /** * @author hyzhou * * 2013-12-18 */ public class StreamRead { public static String readData(InputStream inputStream) throws Exception { ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); byte[] buffer=new byte[1024]; int len=-1; while ((len=inputStream.read(buffer)) !=-1 ){ outputStream.write(buffer,0,len); } byte[] data=outputStream.toByteArray(); outputStream.close(); inputStream.close(); return new String(data); } }
4、添加权限AndroidManifest.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hyzhou.htmlviver" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.hyzhou.htmlviver.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>