Android 网页html源码的查看器

StreamTool.java:

package util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTool {
	/**
	 * 把一个inputstream里面的内容转化成一个byte[]
	 */

	public static byte[] getBytes(InputStream is) throws Exception {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = is.read(buffer)) != -1) {
			bos.write(buffer, 0, len);
		}
		is.close();
		bos.flush();
		byte[] result = bos.toByteArray();
		System.out.println(new String(result));
		return result;
	}
}
NetUtil.java:

package service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import util.StreamTool;

import android.widget.Toast;

public class NetUtil {

	public static String getHtml(String address) throws Exception {
		URL url = new URL(address);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setReadTimeout(5000);
		conn.setRequestMethod("GET");
		int code = conn.getResponseCode();
		if (code == 200) {
			InputStream is = conn.getInputStream();
			byte[] result = StreamTool.getBytes(is);
			return new String(result);
		} else {
			throw new IllegalStateException("访问失败");
		}
	}
}
MainActivity.java:

package com.example.httpviewer;

import service.NetUtil;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	private EditText mEtAddress;
	private Button mBtView;
	private TextView mTvView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mBtView = (Button) this.findViewById(R.id.chakan);
		mEtAddress = (EditText) this.findViewById(R.id.editText1);
		mTvView = (TextView) this.findViewById(R.id.TextView1);
		mBtView.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.chakan:
			// 按钮对应的点击事件
			String address = mEtAddress.getText().toString().trim();
			if ("".equals(address)) {
				Toast.makeText(this, "地址不能为空", Toast.LENGTH_SHORT).show();
				return;
			}
			try {
				String html = NetUtil.getHtml(address);
				mTvView.setText(html);
			} catch (Exception e) {
				e.printStackTrace();
				Toast.makeText(this, "获取数据失败", 0).show();
			}
			break;
		}
	}
}
main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/input_address" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="2"
        android:text="http://www.baidu.com" />

    <Button
        android:id="@+id/chakan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/go" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="center" />
    </ScrollView>

</LinearLayout>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值