HttpUrlConnection Get請求

來源:互聯網
上載者:User

標籤:

  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     <WebView10         android:id="@+id/baidu"11         android:layout_width="match_parent"12         android:layout_height="400dp" />13 14     <Button15         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>

 

HttpUrlConnection Get請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.