亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

【Android Developers Training】 79. 連接到網(wǎng)

系統(tǒng) 2786 0

注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術(shù)一般,由于喜愛安卓而產(chǎn)生了翻譯的念頭,純屬個人興趣愛好。

原文鏈接: http://developer.android.com/training/basics/network-ops/connecting.html


這節(jié)課將會向你展示如何實現(xiàn)一個簡單地連接網(wǎng)絡(luò)的應(yīng)用。這當中包含了一些創(chuàng)建哪怕是最簡單的網(wǎng)絡(luò)連接應(yīng)用時需要遵循的最佳實踐規(guī)范。

注意,要執(zhí)行這節(jié)課中所講的網(wǎng)絡(luò)操作,你的應(yīng)用清單必須包含如下的權(quán)限:

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

一). 選擇一個HTTP客戶端

大多數(shù)帶有網(wǎng)絡(luò)連接的Android應(yīng)用使用HTTP來發(fā)送和接收數(shù)據(jù)。Android包含了兩個HTTP客戶端: HttpURLConnection ,以及 Apache? HttpClient 。兩者都支持HTTPS,數(shù)據(jù)流的上傳和下載,可配置的超時限定,IPv6,以及連接池。對于適用于 Gingerbread及更高系統(tǒng)的應(yīng)用,我們推薦使用 HttpURLConnection 。更多關(guān)于這方面的信息,可以閱讀 Android's HTTP Clients


二). 檢查網(wǎng)絡(luò)連接

當你的應(yīng)用嘗試連接到網(wǎng)絡(luò)時,它應(yīng)該使用 getActiveNetworkInfo() isConnected() 來檢查當前是否有一個可獲取的網(wǎng)絡(luò)連接。這是因為,該設(shè)備可能會超出網(wǎng)絡(luò)的覆蓋范圍,或者用戶將Wi-Fi和移動數(shù)據(jù)連接都禁用的。更多關(guān)于該方面的知識,可以閱讀 Managing Network Usage

      
        public
      
      
        void
      
      
         myClickHandler(View view) {

    ...

    ConnectivityManager connMgr 
      
      =
      
         (ConnectivityManager) 

        getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo 
      
      =
      
         connMgr.getActiveNetworkInfo();

    
      
      
        if
      
       (networkInfo != 
      
        null
      
       &&
      
         networkInfo.isConnected()) {

        
      
      
        //
      
      
         fetch data
      
      

    } 
      
        else
      
      
         {

        
      
      
        //
      
      
         display error
      
      
            }

    ...

}
      
    

三). 在另一個線程上執(zhí)行網(wǎng)絡(luò)操作?

網(wǎng)絡(luò)操作會包含不可預(yù)期的延遲。要避免它引起糟糕的用戶體驗,一般是將網(wǎng)絡(luò)操作在UI線程之外的另一個線程上執(zhí)行。 AsyncTask 類提供了一個最簡單的方法來啟動一個UI線程之外的新線程。想知道有關(guān)這方面的內(nèi)容,可以閱讀: Multithreading For Performance 。

在下面的代碼中, myClickHandler()方法調(diào)用 new DownloadWebpageTask().execute(stringUrl)。 DownloadWebpageTask類是 AsyncTask 的子類,它實現(xiàn)下列 AsyncTask 中的方法:

  • doInBackground() ?- 執(zhí)行 downloadUrl()方法。它接受網(wǎng)頁的URL作為一個參數(shù)。該方法獲取并處理網(wǎng)頁內(nèi)容。當它結(jié)束后,它會返回一個結(jié)果字符串。
  • onPostExecute() ?- 接收返回的字符串并將結(jié)果顯示在UI上。
      
        public
      
      
        class
      
       HttpExampleActivity 
      
        extends
      
      
         Activity {

    
      
      
        private
      
      
        static
      
      
        final
      
       String DEBUG_TAG = "HttpExample"
      
        ;

    
      
      
        private
      
      
         EditText urlText;

    
      
      
        private
      
      
         TextView textView;

    

    @Override

    
      
      
        public
      
      
        void
      
      
         onCreate(Bundle savedInstanceState) {

        
      
      
        super
      
      
        .onCreate(savedInstanceState);

        setContentView(R.layout.main);   

        urlText 
      
      =
      
         (EditText) findViewById(R.id.myUrl);

        textView 
      
      =
      
         (TextView) findViewById(R.id.myText);

    }



    
      
      
        //
      
      
         When user clicks button, calls AsyncTask.

    
      
      
        //
      
      
         Before attempting to fetch the URL, makes sure that there is a network connection.
      
      
        public
      
      
        void
      
      
         myClickHandler(View view) {

        
      
      
        //
      
      
         Gets the URL from the UI's text field.
      
      

        String stringUrl =
      
         urlText.getText().toString();

        ConnectivityManager connMgr 
      
      =
      
         (ConnectivityManager) 

            getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo 
      
      =
      
         connMgr.getActiveNetworkInfo();

        
      
      
        if
      
       (networkInfo != 
      
        null
      
       &&
      
         networkInfo.isConnected()) {

            
      
      
        new
      
      
         DownloadWebpageTask().execute(stringUrl);

        } 
      
      
        else
      
      
         {

            textView.setText(
      
      "No network connection available."
      
        );

        }

    }



     
      
      
        //
      
      
         Uses AsyncTask to create a task away from the main UI thread. This task takes a 

     
      
      
        //
      
      
         URL string and uses it to create an HttpUrlConnection. Once the connection

     
      
      
        //
      
      
         has been established, the AsyncTask downloads the contents of the webpage as

     
      
      
        //
      
      
         an InputStream. Finally, the InputStream is converted into a string, which is

     
      
      
        //
      
      
         displayed in the UI by the AsyncTask's onPostExecute method.
      
      
        private
      
      
        class
      
       DownloadWebpageTask 
      
        extends
      
       AsyncTask<String, Void, String>
      
         {

        @Override

        
      
      
        protected
      
      
         String doInBackground(String... urls) {

              

            
      
      
        //
      
      
         params comes from the execute() call: params[0] is the url.
      
      
        try
      
      
         {

                
      
      
        return
      
       downloadUrl(urls[0
      
        ]);

            } 
      
      
        catch
      
      
         (IOException e) {

                
      
      
        return
      
       "Unable to retrieve web page. URL may be invalid."
      
        ;

            }

        }

        
      
      
        //
      
      
         onPostExecute displays the results of the AsyncTask.
      
      
                @Override

        
      
      
        protected
      
      
        void
      
      
         onPostExecute(String result) {

            textView.setText(result);

       }

    }

    ...

}
      
    

在上述代碼中,事件的發(fā)生流程如下:

當用戶點擊按鈕激活 myClickHandler()后,應(yīng)用會將指定的URL傳遞給 AsyncTask 的子類 DownloadWebpageTask。

AsyncTask 中的方法 doInBackground() 調(diào)用 downloadUrl()方法。

downloadUrl()方法接收一個URL字符串作為參數(shù)并使用它來創(chuàng)建一個 URL 對象。

URL 對象用來建立一個 HttpURLConnection 。

一旦連接建立完成, HttpURLConnection 對象取回網(wǎng)頁內(nèi)容,并將其作為一個 InputStream 。

InputStream 傳遞給 readIt()方法,它將其轉(zhuǎn)換為String。

最后 AsyncTask onPostExecute() 方法將結(jié)果顯示在主activity的UI上。


四). 連接并下載數(shù)據(jù)

在你的執(zhí)行網(wǎng)絡(luò)交互的線程中,你可以使用 HttpURLConnection 來執(zhí)行一個 GET,并下載你的數(shù)據(jù)。在你調(diào)用了 connect()之后,你可以通過調(diào)用 getInputStream()來 獲得數(shù)據(jù)的 InputStream 。

在下面的代碼中, doInBackground() 方法調(diào)用 downloadUrl()。后者接收URL參數(shù),并使用URL通過 HttpURLConnection 連接網(wǎng)絡(luò)。一旦一個連接建立了,應(yīng)用將使用 getInputStream()方法來獲取 InputStream 形式的數(shù)據(jù)。

      
        //
      
      
         Given a URL, establishes an HttpUrlConnection and retrieves


      
      
        //
      
      
         the web page content as a InputStream, which it returns as


      
      
        //
      
      
         a string.
      
      
        private
      
       String downloadUrl(String myurl) 
      
        throws
      
      
         IOException {

    InputStream is 
      
      = 
      
        null
      
      
        ;

    
      
      
        //
      
      
         Only display the first 500 characters of the retrieved

    
      
      
        //
      
      
         web page content.
      
      
        int
      
       len = 500
      
        ;

        

    
      
      
        try
      
      
         {

        URL url 
      
      = 
      
        new
      
      
         URL(myurl);

        HttpURLConnection conn 
      
      =
      
         (HttpURLConnection) url.openConnection();

        conn.setReadTimeout(
      
      10000 
      
        /*
      
      
         milliseconds 
      
      
        */
      
      
        );

        conn.setConnectTimeout(
      
      15000 
      
        /*
      
      
         milliseconds 
      
      
        */
      
      
        );

        conn.setRequestMethod(
      
      "GET"
      
        );

        conn.setDoInput(
      
      
        true
      
      
        );

        
      
      
        //
      
      
         Starts the query
      
      
                conn.connect();

        
      
      
        int
      
       response =
      
         conn.getResponseCode();

        Log.d(DEBUG_TAG, 
      
      "The response is: " +
      
         response);

        is 
      
      =
      
         conn.getInputStream();



        
      
      
        //
      
      
         Convert the InputStream into a string
      
      

        String contentAsString =
      
         readIt(is, len);

        
      
      
        return
      
      
         contentAsString;

        

    
      
      
        //
      
      
         Makes sure that the InputStream is closed after the app is

    
      
      
        //
      
      
         finished using it.
      
      

    } 
      
        finally
      
      
         {

        
      
      
        if
      
       (is != 
      
        null
      
      
        ) {

            is.close();

        } 

    }

}
      
    

注意方法getResponseCode()返回的是連接的狀態(tài)碼( status code )。這是一個非常有用的方法來獲得關(guān)于連接的額外信息。狀態(tài)碼200意味著連接成功。


五). 將InputStream轉(zhuǎn)換為String

一個 InputStream 是一個可讀的字節(jié)源。一旦你獲得了一個 InputStream ,通常都需要將它解碼或轉(zhuǎn)換成你需要的數(shù)據(jù)類型。例如,如果你正在下載圖像數(shù)據(jù),你可能會這樣對它進行解碼:

      InputStream is = 
      
        null
      
      
        ;

...

Bitmap bitmap 
      
      =
      
         BitmapFactory.decodeStream(is);

ImageView imageView 
      
      =
      
         (ImageView) findViewById(R.id.image_view);

imageView.setImageBitmap(bitmap);
      
    

在上面的例子中, InputStream 代表了一個網(wǎng)頁的文本。下面的例子是將 InputStream 轉(zhuǎn)換為String,這樣activity可以在UI中顯示它:

      
        //
      
      
         Reads an InputStream and converts it to a String.
      
      
        public
      
       String readIt(InputStream stream, 
      
        int
      
       len) 
      
        throws
      
      
         IOException, UnsupportedEncodingException {

    Reader reader 
      
      = 
      
        null
      
      
        ;

    reader 
      
      = 
      
        new
      
       InputStreamReader(stream, "UTF-8"
      
        );        

    
      
      
        char
      
      [] buffer = 
      
        new
      
      
        char
      
      
        [len];

    reader.read(buffer);

    
      
      
        return
      
      
        new
      
      
         String(buffer);

}
      
    

【Android Developers Training】 79. 連接到網(wǎng)絡(luò)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日本伊人色综合网站 | www黄com | 日韩国产欧美在线观看 | 国产精品嫩草影院99av视频 | 99精品在线视频观看 | 91亚洲国产三上悠亚在线播放 | 又黄又爽又色的免费毛片 | 国产第一福利 | 女性特黄一级毛片 | 亚洲精品永久一区 | 欧美日本视频一区 | 久久久高清国产999尤物 | 日韩精品一区二区三区乱码 | 热久久亚洲 | 精品一区二区久久久久久久网站 | 伊人婷婷色 | 2021午夜国产精品福利 | 欧美特黄a级高清免费大片 欧美特黄a级猛片a级 | 久久一级 | 牛牛色婷婷在线视频播放 | 亚洲视频一区在线 | 精品国产一区二区三区香蕉沈先生 | 国产真实伦视频在线观看 | 9久久免费国产精品特黄 | 国产国语高清在线视频二区 | 亚洲精品欧洲精品 | 久久这里只有精品1 | 加勒比一本 | 日韩精品视频一区二区三区 | 国产中文字幕在线 | 欧洲免费在线视频 | 国产精品分类视频分类一区 | 久久成人免费网站 | 欧美日韩顶级毛片www免费看 | 国产不卡精品一区二区三区 | 日韩一及片 | 欧美亚洲国产激情一区二区 | 欧美乱妇高清无乱码视频在线 | 成人a毛片免费视频观看 | 中文在线免费不卡视频 | 免费的一级片网站 |