Android網路連接處理學習筆記

來源:互聯網
上載者:User

在Android中,可以有多種方式來實現網路編程:

建立URL,並使用URLConnection/HttpURLConnection 

使用HttpClient
 
使用WebView

建立URL,並使用URLConnection/HttpURLConnection

java.net.*下面提供了訪問 HTTP 服務的準系統。使用這部分介面的基本操作主要包括:

建立 URL 以及 URLConnection / HttpURLConnection 對象

1 設定串連參數
 
2 串連到伺服器

3 向伺服器寫資料

4 從伺服器讀取資料

源碼:

  1. try {  
  2.         // 建立URL對象  
  3.         URL url = new URL("http://t.sina.cn/fesky");  
  4.         // 建立URL串連  
  5.         URLConnection connection = url.openConnection();  
  6.         // 對於 HTTP 串連可以直接轉換成 HttpURLConnection,  
  7.         // 這樣就可以使用一些 HTTP 串連特定的方法,如 setRequestMethod() 等  
  8.         // HttpURLConnection connection  
  9.         // =(HttpURLConnection)url.openConnection(Proxy_yours);  
  10.         // 設定參數  
  11.         connection.setConnectTimeout(10000);  
  12.         connection.addRequestProperty("User-Agent", "J2me/MIDP2.0");  
  13.         // 串連伺服器  
  14.         connection.connect();  
  15.  
  16.     } catch (IOException e) {  
  17.         // TODO Auto-generated catch block  
  18.         e.printStackTrace();  
  19.     } 

使用HttpClient
對於HttpClient類,可以使用HttpPost和HttpGet類以及HttpResponse來進行網路連接。

 

使用WebView

Android手機中內建了一款高效能webkit核心瀏覽器,在SDK中封裝成了WebView組件。

http://developer.android.com/guide/tutorials/views/hello-webview.html提供了一個簡單的例子:

1. webview的XML定義:

  1. <WebView    
  2.         android:id="@+id/webview"   
  3.         android:layout_width="fill_parent"   
  4.         android:layout_height="fill_parent"   
  5.     />  

2.Manifest檔案中許可權的設定:

  1. <uses-permission android:name="android.permission.INTERNET" /> 

3.如果想要支援JavaScript:

  1. webview.getSettings().setJavaScriptEnabled(true);   

4.如果需要在WebView中顯示網頁,而不是在內建瀏覽器中瀏覽,則需要mWebView.setWebViewClient,並重寫shouldOverrideUrlLoading方法。

5.如果不做任何處理,在顯示你的Brower UI時,點擊系統“Back”鍵,整個Browser會作為一個整體“Back"到其他Activity中,而不是希望的在Browser的曆史頁面中Back。如果希望實現在曆史頁面中Back,需要在當前Activity中處理Back事件:mWebView.goBack();

  1. WebView webview;  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.         // 擷取WebView對象  
  8.         webview = (WebView) findViewById(R.id.webview);   
  9.         // 使能JavaScript  
  10.         webview.getSettings().setJavaScriptEnabled(true);   
  11.         // 如果需要在WebView中顯示網頁,而不是在內建瀏覽器中瀏覽,  
  12.         // 則需要mWebView.setWebViewClient,並重寫  
  13.         // shouldOverrideUrlLoading方法。  
  14.         webview.setWebViewClient(new WebViewClientDemo());  
  15.         // 載入網頁  
  16.         webview.loadUrl("http://t.sina.cn/fesky");    
  17.     }  
  18.     @Override  
  19.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  20.         // 按下BACK鍵回到曆史頁面中  
  21.         if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {   
  22.             webview.goBack();   
  23.             return true;   
  24.         }   
  25.         return super.onKeyDown(keyCode, event);  
  26.     }  
  27.     private class WebViewClientDemo extends WebViewClient {   
  28.         @Override   
  29.         // 在WebView中而不是預設瀏覽器中顯示頁面  
  30.         public boolean shouldOverrideUrlLoading(WebView view, String url) {   
  31.             view.loadUrl(url);   
  32.             return true;   
  33.         }   
  34.     } 

以上是採用loadUrl方法實現網頁的載入,也可以採用loadData或者loadDataWithBaseURL方法實現網頁的載入:

webview.loadData(html, “text/html”, "utf-8”);

如果html中包含中文,則需要webview.loadData(URLEncoder.encode(html,encoding), mimeType, encoding);

對於本地圖片或網頁的顯示,可以使用loadUrl,不過Url的地址首碼為file:///,如"file:///android_asset/test.htm”。

Android布局屬性詳解

Android智能手機作業系統

Android應用程式開發環境的搭建

從Android應用領域看智能終端的未來

聯繫我們

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