欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android 網(wǎng)絡(luò)連接處理分析

 更新時間:2012年11月22日 16:07:07   作者:  
在Android中,可以有多種方式來實現(xiàn)網(wǎng)絡(luò)編程,本文將詳細(xì)介紹android 網(wǎng)絡(luò)連接處理,需要了解的朋友可以參考下

在Android中,可以有多種方式來實現(xiàn)網(wǎng)絡(luò)編程:

創(chuàng)建URL,并使用URLConnection/HttpURLConnection

使用HttpClient

使用WebView

創(chuàng)建URL,并使用URLConnection/HttpURLConnection

java.net.*下面提供了訪問 HTTP 服務(wù)的基本功能。使用這部分接口的基本操作主要包括:

創(chuàng)建 URL 以及 URLConnection / HttpURLConnection 對象

1 設(shè)置連接參數(shù)

2 連接到服務(wù)器

3 向服務(wù)器寫數(shù)據(jù)

4 從服務(wù)器讀取數(shù)據(jù)

源碼

try { 
        // 創(chuàng)建URL對象 
        URL url = new URL("http://t.sina.cn/fesky"); 
        // 創(chuàng)建URL連接 
        URLConnection connection = url.openConnection(); 
        // 對于 HTTP 連接可以直接轉(zhuǎn)換成 HttpURLConnection, 
        // 這樣就可以使用一些 HTTP 連接特定的方法,如 setRequestMethod() 等 
        // HttpURLConnection connection 
        // =(HttpURLConnection)url.openConnection(Proxy_yours); 
        // 設(shè)置參數(shù)  www.dbjr.com.cn
        connection.setConnectTimeout(10000); 
        connection.addRequestProperty("User-Agent", "J2me/MIDP2.0"); 
        // 連接服務(wù)器 
        connection.connect(); 

    } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
    }
使用HttpClient
對于HttpClient類,可以使用HttpPost和HttpGet類以及HttpResponse來進(jìn)行網(wǎng)絡(luò)連接。

 

使用WebView

Android手機(jī)中內(nèi)置了一款高性能webkit內(nèi)核瀏覽器,在SDK中封裝成了WebView組件。

1. webview的XML定義:

<WebView   
        android:id="@+id/webview"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
    /> 
2.Manifest文件中權(quán)限的設(shè)定:
<uses-permission android:name="android.permission.INTERNET" />
3.如果想要支持JavaScript:webview.getSettings().setJavaScriptEnabled(true);  
4.如果需要在WebView中顯示網(wǎng)頁,而不是在內(nèi)置瀏覽器中瀏覽,則需要mWebView.setWebViewClient,并重寫shouldOverrideUrlLoading方法。

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

WebView webview; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        // 獲取WebView對象 
        webview = (WebView) findViewById(R.id.webview);  
        // 使能JavaScript 
        webview.getSettings().setJavaScriptEnabled(true);  
        // 如果需要在WebView中顯示網(wǎng)頁,而不是在內(nèi)置瀏覽器中瀏覽, 
        // 則需要mWebView.setWebViewClient,并重寫 
        // shouldOverrideUrlLoading方法。 
        webview.setWebViewClient(new WebViewClientDemo()); 
        // 加載網(wǎng)頁 
        webview.loadUrl("http://t.sina.cn/fesky");   
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        // 按下BACK鍵回到歷史頁面中 
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {  
            webview.goBack();  
            return true;  
        }  
        return super.onKeyDown(keyCode, event); 
    } 
    private class WebViewClientDemo extends WebViewClient {  
        @Override  
        // 在WebView中而不是默認(rèn)瀏覽器中顯示頁面 
        public boolean shouldOverrideUrlLoading(WebView view, String url) {  
            view.loadUrl(url);  
            return true;  
        }  
    }
webview.loadData(html, "text/html", "utf-8");

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

對于本地圖片或網(wǎng)頁的顯示,可以使用loadUrl,不過Url的地址前綴為file:///,如"file:///android_asset/test.htm"。

相關(guān)文章

最新評論