网页源码查看实例

package cn.captain.htmlviewer;


import cn.captain.service.htmlService;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class HtmlViwerActivity extends Activity {
	private static final String TAG = "ImageViewActivity";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button =(Button) this.findViewById(R.id.button);
       final TextView urlpath = (TextView)this.findViewById(R.id.urlpath);
       final TextView text = (TextView)this.findViewById(R.id.text);
       button.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String path  = urlpath.getText().toString();
			 try {
					text.setText(htmlService.getHtml(path));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Toast.makeText(HtmlViwerActivity.this,R.string.error,1).show();
					Log.e(TAG, e.toString());
				}
		}
	});
      
       
    }
}

htmlService.java

 

package cn.captain.service;

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

import cn.captain.htmlviewer.R;
import cn.captain.utils.StreamTool;

public class htmlService {

	public static String getHtml(String path) throws Exception
	{
		URL url = new URL(path); 
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(5 * 1000);
		InputStream inStream =  conn.getInputStream();//通过输入流获取html数据	
		byte[] data = StreamTool.readInputStream(inStream);//得到html的二进制数据
		String html = new String(data);
		return html;
	}
}

StreamTool.java

 

package cn.captain.utils;

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

public class StreamTool {
	public static byte[] readInputStream(InputStream instream) throws Exception
    {
	ByteArrayOutputStream outStream = new ByteArrayOutputStream();
	byte[]  buffer = new byte[1204];
	int len = 0;
	while ((len = instream.read(buffer)) != -1)
	{
		outStream.write(buffer,0,len);
	}
	instream.close();
	return outStream.toByteArray();     	
	}
}

main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/urlpath" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="http://www.sohu.com/"
        android:id="@+id/urlpath"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" 
        android:id="@+id/button"/>
 <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" 
        android:id="@+id/text"
        />
   </ScrollView>
</LinearLayout>

strings.xml

 

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello"></string>
    <string name="app_name">网页源码查看器</string>
    <string name="error">连接超时</string>
    <string name="urlpath">网页地址</string>
    <string name="button">查看源码</string>

</resources>

网页源码查看实例

你可能感兴趣的:(网络获取THML)