从网络上获取网页源代码

package cn.itcast.htmlviewer;

import cn.itcast.htmlviewer.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.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DemoActivity 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.bt_view);
        mEtAddress =  (EditText) this.findViewById(R.id.et_address);
        mTvView =  (TextView) this.findViewById(R.id.tv_content);
        mBtView.setOnClickListener(this);
        
        
    }
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_view:
			//按钮对应的点击事件 
			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;

		}
		
	}
}
<pre class="java" name="code">package cn.itcast.htmlviewer.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;
	}
}
<pre class="java" name="code">package cn.itcast.htmlviewer.service;

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

import cn.itcast.htmlviewer.util.StreamTool;

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);
			String temp = new String(result);
			// 简单描述原理
			// 真实的代码需要解析meta里面的信息
			if (temp.contains("gbk")) {
				return new String(result, "gb2312");
			} else {
				return temp;
			}
		} else {
			throw new IllegalStateException("访问网络失败");
		}
	}
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值