android中的HttpUrlConnection的使用之一

來源:互聯網
上載者:User

標籤:

在我們之前學習Java的網路編程當中,我們已經見過HttpUrlConnection類的使用。HttpUrlConnect在Java是一個支援http特定的功能一個類,在許多的網路編程經常使用到它。今天我在這裡記錄的是android中使用HttpUrlConnection,之前我還學過使用非同步載入來載入一個網站,它使用的原理同樣是多線程,但是它使用的是用json來解析一個網站,而不是直接使用網域名稱也就是網址來解析一個網站,而將要介紹的HttpUrlConnection是使用網域名稱來解析一個網站的,當然它同時使用到了handler類,因為這個案例使用到了子線程來載入ui,主線程來更新ui。代碼如下

xml代碼

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" 6     tools:context="com.example.android_http.MainActivity" > 7     <WebView  8         android:id="@+id/webview" 9         android:layout_width="fill_parent"10         android:layout_height="fill_parent"11         />12 13 </LinearLayout>

從上面的xml中可以看出,我們只用了一個控制項,那就是WebView。webview在載入網站當中自有它自己的妙處。

java代碼

1.首先是Mainactivity類

 1 package com.example.android_http; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.Handler; 6 import android.webkit.WebView; 7  8 public class MainActivity extends Activity { 9     private WebView webview = null;10     private Handler handler = new Handler();11     @Override12     protected void onCreate(Bundle savedInstanceState) {13         super.onCreate(savedInstanceState);14         setContentView(R.layout.activity_main);15         webview = (WebView) findViewById(R.id.webview);16         new HttpThread("http://www.baidu.com", webview, handler).start();17     }18 19 }

2.其次是HttpThread類,這個繼承類Thread類,建立了一個子線程來載入網頁

 1 package com.example.android_http; 2  3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 import java.net.MalformedURLException; 8 import java.net.ProtocolException; 9 import java.net.URL;10 11 import javax.net.ssl.HttpsURLConnection;12 13 import android.os.Handler;14 import android.webkit.WebView;15 16 public class HttpThread  extends Thread{17     private String url = null;18     private WebView webview = null;19     private Handler handler = null;20     public HttpThread(String url, WebView webview, Handler handler)21     {22         this.handler = handler;23         this.url = url;24         this.webview = webview;25     }26     @Override27     public void run() {28         /*29          * 關於HttpUrlConnection的使用,有什麼不懂,請回去認真複習一下Java的網路編程30          */31         HttpURLConnection httpurlconnection = null;32         try {33             URL url = new URL(this.url); 34             httpurlconnection = (HttpURLConnection) url.openConnection();35         } catch (MalformedURLException e) {36             // TODO Auto-generated catch block37             e.printStackTrace();38         } catch (IOException e) {39             // TODO Auto-generated catch block40             e.printStackTrace();41         }42         //允許逾時5秒43         try {44             httpurlconnection.setReadTimeout(5000);45             //以get的方式與伺服器相串連46             httpurlconnection.setRequestMethod("GET");47         } catch (ProtocolException e) {48             // TODO Auto-generated catch block49             e.printStackTrace();50         }51         final StringBuffer sb = new StringBuffer();52         String str = null;53         try {54             BufferedReader br = new BufferedReader(new InputStreamReader(httpurlconnection.getInputStream()));55             try {56                 while((str = br.readLine()) != null)57                 {58                     sb.append(str);59                 }60             } catch (IOException e) {61                 // TODO Auto-generated catch block62                 e.printStackTrace();63             }64         } catch (IOException e) {65             // TODO Auto-generated catch block66             e.printStackTrace();67         }68         handler.post(new Runnable() {69 70             public void run() {71                 //第一個參數表示的是需要載入的資料--這個先前已經在輸入資料流讀入到了sb中72                 //第二參數表示的是需要載入網站的編碼格式--百度的編碼格式就是如此73                 /*74                  * 需要注意的是:75                  * loaddata不載入圖片,如果想要載入圖片,請使用loadDataWithBaseURL76                  */77                 webview.loadData(sb.toString(), "text/html;charset=utf-8", null);78                 79             }80         });81     }82 83 }

 

android中的HttpUrlConnection的使用之一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.