关闭

Android开发 通过URL获取网页源代码(滚动显示)

150人阅读 评论(0) 收藏 举报

layout

*注意TextView滚动条的实现

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

    </ScrollView>


</LinearLayout>


MainActivity

package com.test.URLtest;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText pathText;
    private TextView codeView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
        Button button = (Button) this.findViewById(R.id.button);
        pathText = (EditText) this.findViewById(R.id.url);
        codeView = (TextView) this.findViewById(R.id.codeView);
        button.setOnClickListener(new ButtonClickListener());
    }
    private final class ButtonClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            String path = pathText.getText().toString();
            try {
                String html = PageService.getHtml(path);
                codeView.setText(html);
            }
            catch(Exception e){
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),R.string.error,Toast.LENGTH_SHORT).show();

            }

        }
    }
}

PageService业务类

package com.test.URLtest;

import javax.net.ssl.HttpsURLConnection;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by 宝冬 on 2015/7/9.
 */
public class PageService {
    public static String getHtml(String path) throws Exception{
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if(conn.getResponseCode()==200){
            InputStream inStream = conn.getInputStream();
            byte[] data = StreamTool.read(inStream);
            String html = new String(data,"UTF-8");
            return html;
        }
        return null;
    }
}

StreamTool工具类
package com.test.URLtest;

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


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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2986次
    • 积分:221
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档