Android开发入门之网络通信(网页源码查看器)

第一步:新建一个Android工程命名为htmlViewer目录结构如下图:

Android开发入门之网络通信(网页源码查看器)_第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" >

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

    <EditText
        android:id="@+id/et_path"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/path" />

    <Button
        android:id="@+id/btn_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/view" />

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

        <TextView
            android:id="@+id/tv_code_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>

strings.xml:

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

    <string name="app_name">网页源码查看器</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="htmlPath">网页地址</string>
    <string name="view">查看</string>
    <string name="success">获取源码成功!</string>
    <string name="fail">获取源码失败!</string>
    <string name="path">http://www.so.com</string>

</resources>

第三步:编写MianActivity类:

package cn.leigo.htmlviewer;

import java.io.IOException;

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;
import cn.leigo.service.CodeService;

public class MainActivity extends Activity implements OnClickListener {
	private EditText mHtmlPathEditText;
	private Button mViewButton;
	private TextView mCodeViewTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mHtmlPathEditText = (EditText) findViewById(R.id.et_path);
		mViewButton = (Button) findViewById(R.id.btn_view);
		mCodeViewTextView = (TextView) findViewById(R.id.tv_code_view);
		mViewButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		String path = mHtmlPathEditText.getText().toString();
		String code;
		try {
			code = CodeService.getHtml(path);
			mCodeViewTextView.setText(code);
			Toast.makeText(this, R.string.success, Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			e.printStackTrace();
			Toast.makeText(this, R.string.fail, Toast.LENGTH_SHORT).show();
		}

	}
}

业务类CodeService:

package cn.leigo.service;

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

import javax.net.ssl.HttpsURLConnection;

import cn.leigo.utils.StreamTool;

public class CodeService {

	/**
	 * 获取网页HTML源代码
	 * 
	 * @param path
	 *            网页路径
	 * @return
	 * @throws IOException
	 */
	public static String getHtml(String path) throws IOException {
		String code = "";
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		if (conn.getResponseCode() == HttpsURLConnection.HTTP_OK) {
			InputStream inputStream = conn.getInputStream();
			byte[] data = StreamTool.read(inputStream);
			code = new String(data, "UTF-8");
		}
		return code;
	}

}

工具类StreamTool:

package cn.leigo.utils;

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

public class StreamTool {
	/**
	 * 读取流中的数据
	 * 
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public static byte[] read(InputStream inputStream) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inputStream.read(buffer)) != -1) {
			baos.write(buffer, 0, len);
		}
		inputStream.close();
		return baos.toByteArray();
	}

}

最后别忘了在AndroidManifest.xml文件中添加权限:

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

运行上述工程查看效果图:

Android开发入门之网络通信(网页源码查看器)_第2张图片

你可能感兴趣的:(android,网络,网页源码查看器)