【实战】android网页源代码查看器

点击查看源代码,可以查看该网页的源代码

一、文件结构


MainActivity.java:

package com.example.viewpagesource;


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


import com.example.viewpagesource.utils.StreamTool;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
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 View.OnClickListener {


	protected static final int SUCCESS = 0;
	protected static final int ERROR = 1;
	protected static final int NETWORK_ERROR = 2;
	private Button btn;
	private EditText ed_path;
	private TextView tv;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);
        ed_path=(EditText) findViewById(R.id.ed_path);
        tv=(TextView) findViewById(R.id.tv);
    }


    //定义一个秘书
    Handler handler=new Handler(){
    	public void handleMessage(android.os.Message msg) {
    		switch (msg.what) {
			case SUCCESS:
				String value=(String) msg.obj;
				tv.setText(value);
				break;
			case ERROR:
				System.out.println("ERROR");
				Toast.makeText(MainActivity.this, "错误发生了", 0).show();
				break;
			case NETWORK_ERROR:
				System.out.println("NETWORK_ERROR");
				Toast.makeText(MainActivity.this, "错误发生了", 0).show();
				break;
			default:
				break;
			}
    	};
    };
  




    String path;
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		path = ed_path.getText().toString().trim();
		if(TextUtils.isEmpty(path)){
			Toast.makeText(this, "路径有错误", 0).show();
			return;
		}
		
		//连接网络,要启动一个新的线程去,干耗时的事情
		new Thread(){
			public void run() {
				try {
					URL url=new URL(path);
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();
					//设置连接超时为5秒钟
					conn.setConnectTimeout(5000);
					conn.setRequestMethod("GET");
					String contentType = conn.getContentType();
					
					
					int code = conn.getResponseCode();
					if(code==200){
						InputStream in = conn.getInputStream();
						
						//这里的data是从服务器返回的
						String data=StreamTool.decodeStream(in);
						
						//展示在屏幕上
						Message msg=Message.obtain();
						msg.what=SUCCESS;
						msg.obj=data;
						
						handler.sendMessage(msg);
					}else{
						Message msg=Message.obtain();
						msg.what=ERROR;
						
						
						handler.sendMessage(msg);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Message msg=Message.obtain();
					msg.what=NETWORK_ERROR;
					
					
					handler.sendMessage(msg);
					
					e.printStackTrace();
				}
			};
		}.start();
	}
    
}

StreamTool.java

package com.example.viewpagesource.utils;


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


public class StreamTool {


	public static String decodeStream(InputStream in) throws IOException {
		// TODO Auto-generated method stub
		//底层流
		
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
			
			int len=0;
			byte[] buf=new byte[1024];
			while((len=in.read(buf))>0){
				baos.write(buf,0,len);
			}
			
			String data = baos.toString();
			return data;
		
	}


}

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/ed_path"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="http://www.baidu.com" />
	<Button
	    android:id="@+id/btn" 
	    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="查看网页源代码"/>
	<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
	<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:id="@+id/tv" />
	</ScrollView>
</LinearLayout>

AndroidMainfest.xml

<uses-permission android:name="android.permission.INTERNET"/>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mind_programmonkey

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值