您的位置:首页 > 理论基础 > 计算机网络

android的Http协议学习与应用——连接一个网页

2015-08-08 23:00 661 查看
[b]MainActivity主类
[/b]

package com.example.http_01;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.webkit.WebView;

public class MainActivity extends Activity {

    private WebView webView;

    private Handler handler = new Handler();

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webView1);

        //调用start方法开启线程

        new HttpThread("https://www.baidu.com/", webView, handler).start();

    }

}

HttpThread类

package com.example.http_01;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import android.os.Handler;

import android.webkit.WebView;

public class HttpThread extends Thread{

    private String url;

    private WebView webView;

    private Handler handler;

    

    public HttpThread(String url,WebView webView,Handler handler){

        this.url = url;

        this.webView = webView;

        this.handler = handler;

    }

    @Override

    public void run() {

        try {

            //实例化URL对象,统一资源定位符对象

            URL httpUrl = new URL(url);

            try {

                HttpsURLConnection conn = (HttpsURLConnection) httpUrl.openConnection();

                //设置请求超时的时间

                conn.setReadTimeout(5000);

                //设置请求方式

                conn.setRequestMethod("GET");

                //创建stringbuffer对象来缓存数据

                final StringBuffer sb = new StringBuffer();

                //conn.getInputStream()代表网页返回的字符流,InputStreamReader将字符流转化成字节流

                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                String str;

                while ((str = reader.readLine())!=null) {

                    //将读取的每一行字节流存入StringBuffer中

                    sb.append(str);

                }

                handler.post(new Runnable(){

                    @Override

                    public void run() {

                        //加载数据

                        webView.loadData(sb.toString(), "text/html;charset=utf-8", null);

                    }

                });

            } catch (IOException e) {

                e.printStackTrace();

            }

            

        } catch (MalformedURLException e) {

            e.printStackTrace();

        }

    }
}

显示页面配置文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/container"

    and
4000
roid:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.example.http_01.MainActivity"

    tools:ignore="MergeRootFrame" >

    <WebView

        android:id="@+id/webView1"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true" />

    

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: