1、编写activity_main.xml

网页源代码查看器
<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>
View Code

2、编写MainActivity.java

网页源代码查看器
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();        }    }}
View Code

3、编写读取html数据类StreamRead.java

网页源代码查看器
/** *  */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);            }}
View Code

4、添加权限AndroidManifest.xml

网页源代码查看器
<?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>
View Code

demo下载

更多相关文章

  1. android source code下载源代码时出错
  2. Android source code[ 源代码]下载,问题和解决办法
  3. android调用浏览器打开网页链接
  4. 在Eclipse 中关联android sdk 源代码
  5. Android 网页无法打开 net:ERR_UNKNOWN_URL_SCHEME
  6. android 根据SD卡中图片路径读取并显示SD中的图片——源代码
  7. Android 源代码在线阅读
  8. Android:获取网页源代码
  9. android 根据SD卡中图片路径读取并显示SD中的图片――源代码

随机推荐

  1. Android概述
  2. android framework 图解分享
  3. Android(安卓)HTTP实例 发送请求和接收响
  4. Android(安卓)中service 详解
  5. 如何删除Android自带的应用程序?
  6. Android(安卓)布局 & 一些控件
  7. Android(安卓)蓝牙4.0代码解析
  8. Android(安卓)Studio 错误:Invalid Androi
  9. Android(安卓)Studio 初体验
  10. Android-AbsoluteLayout(绝对布局)