HttpUrlConnection Get请求

  1 package com.example.administrator.myapplication.activity;
  2 
  3 import android.os.Bundle;
  4 import android.os.Handler;
  5 import android.os.Message;
  6 import android.support.v7.app.AppCompatActivity;
  7 import android.view.View;
  8 import android.webkit.WebView;
  9 import android.widget.Button;
 10 
 11 import com.example.administrator.myapplication.R;
 12 
 13 import java.io.IOException;
 14 import java.io.InputStream;
 15 import java.net.HttpURLConnection;
 16 import java.net.MalformedURLException;
 17 import java.net.URL;
 18 
 19 public class HttpUrlConnectionGetActivity extends AppCompatActivity {
 20     WebView webView;
 21     Button httpUrlBtn;
 22     @Override
 23     protected void onCreate(Bundle savedInstanceState) {
 24         super.onCreate(savedInstanceState);
 25         setContentView(R.layout.activity_get);
 26         webView = (WebView) findViewById(R.id.baidu);
 27         httpUrlBtn = (Button) findViewById(R.id.HttpUrl);
 28         httpUrlBtn.setOnClickListener(new View.OnClickListener() {
 29             @Override
 30             public void onClick(View v) {
 31                 /*Thread thread = new Thread(new Runnable() {
 32                     @Override
 33                     public void run() {
 34                         HttpURLConnectionGet();
 35                     }
 36                 });
 37                 thread.start();*/
 38                 //!!!网络请求HttpURLConnectionGet()要放在子线程中
 39                 new Thread(new Runnable() {
 40                     @Override
 41                     public void run() {
 42                         HttpURLConnectionGet();
 43                     }
 44                 }).start();
 45             }
 46         });
 47     }
 48     //get请求
 49     private void HttpURLConnectionGet() {
 50         HttpURLConnection httpURLConnection = null;
 51         InputStream is = null;
 52         //StringBuilder:线程非安全,可有多线程采用,速度比StingBuffer快,用法同StringBuffer
 53         // StringBuffer:线程安全,只能单线程采用
 54         StringBuilder sb = new StringBuilder();
 55         try {
 56             //准备请求的网络地址
 57             URL url = new URL("http://apis.baidu.com/txapi/weixin/wxhot?num=10&page=1&word=%E7%9B%97%E5%A2%93%E7%AC%94%E8%AE%B0");
 58             //调用openConnection得到网络连接,网络连接处于就绪状态
 59             httpURLConnection = (HttpURLConnection) url.openConnection();
 60             //设置网络连接超时时间5S
 61             httpURLConnection.setConnectTimeout(5*1000);
 62             //设置读取超时时间
 63             httpURLConnection.setReadTimeout(5*1000);
 64             httpURLConnection.setRequestProperty("apikey","58218dcc8845195b277082c3a357f481");
 65             httpURLConnection.connect();
 66             //if连接请求码成功
 67             if (httpURLConnection.getResponseCode() == httpURLConnection.HTTP_OK){
 68                 is = httpURLConnection.getInputStream();
 69                 byte[] bytes = new byte[1024];
 70                 int i = 0;
 71                 while ((i = is.read(bytes)) != -1){
 72                     sb.append(new String(bytes,0,i,"utf-8"));
 73                 }
 74                 is.close();
 75             }
 76         } catch (MalformedURLException e) {
 77             e.printStackTrace();
 78         } catch (IOException e) {
 79             e.printStackTrace();
 80         }finally {
 81             if (httpURLConnection != null){
 82                 httpURLConnection.disconnect();
 83             }
 84         }
 85         //发送消息:what消息类型,obj消息内容
 86         Message message = handler.obtainMessage(1,sb.toString());
 87         handler.sendMessage(message);
 88     }
 89 
 90     //Handler:消息处理机制(发消息,处理消息),只能放在主线程中
 91     Handler handler = new Handler(){
 92         @Override
 93         public void handleMessage(Message msg) {
 94             super.handleMessage(msg);
 95             if (msg != null && msg.what ==1){
 96                 String s = (String) msg.obj;
 97                 webView.getSettings().setDefaultTextEncodingName("utf-8");
 98                 webView.getSettings().setJavaScriptEnabled(true);
 99                 webView.loadDataWithBaseURL(null,s,"text/html","utf-8",null);
100             }
101         }
102     };
103 }
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context=".activity.HttpUrlConnectionGetActivity">
 8 
 9     <WebView
10         android:id="@+id/baidu"
11         android:layout_width="match_parent"
12         android:layout_height="400dp" />
13 
14     <Button
15         android:id="@+id/HttpUrl"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:text="HttpUrlConnection Get请求"
19         android:textAllCaps="false" />
20 
21 </LinearLayout>

 

posted @ 2016-09-20 17:48  me丶不是好人  阅读(5014)  评论(0编辑  收藏  举报