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

MainActivity主类

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"
    android: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>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值