Android Volley 庫通過網路擷取 JSON 資料

來源:互聯網
上載者:User

本文內容

  • 什麼是 Volley 庫
  • Volley 能做什麼
  • Volley 架構
  • 示範 Volley 庫通過網路擷取 JSON 資料
  • 參考資料

Android 關於網路操作一般都會介紹 HttpClient 以及 HttpConnection 這兩個包。前者是 Apache 開源庫,後者是 Android 內建 API。企業級應用,一般都會選擇使用已經封裝好的 http 架構。比較流行有 Volley、android-async-http、retrofit、okhttp、androidquery、AndroidAsync 等。他們各有特點,不同的架構有不同的效率,如今 Google 推出了官方的針對 Android 平台上的網路通訊庫 Volley,能使網路通訊更快,更簡單,更健壯,Volley 在提供了高效能網路通訊功能的同時,對網狀圖片載入也提供了良好的支援,完全可以滿足簡單 REST 用戶端的需求。另外,但 Volley 擴充性很強,可以根據需要定製你自己的網路請求。

什麼是 Volley 庫

Volley 是 Ficus Kirpatrick 在 Gooogle I/O 2013 發布的一個處理和緩衝網路請求的庫,能使網路通訊更快,更簡單,更健壯。Volley 名稱的由來 a burst or emission of many things or a large amount at once。在Gooogle I/O 2013 演講上,其配圖是一幅發射火弓箭。 1 所示。

圖 1 Gooogle I/O 2013 Volley

Volley 管理著處理和緩衝網路請求,它節省了開發人員的寶貴時間,一次又一次編寫相同的網路調用/緩衝代碼。更少的重複代碼,其中一個好處就是減少了 bug,這是所有開發人員所期望的。

所謂“一次又一次編寫相同的網路調用/緩衝代碼”,就是 AsyncTask 以及你編寫的通過 Web API 擷取響應,並顯示出來的邏輯/代碼。我們必須在 ProgressBar() 和 onPostExecute() 方法內照顧  ProgressBar/ProgressDialog 。雖然這不是一個艱巨的任務,但卻很無聊,甚至感到厭倦,儘管我已經定義了 BaseTask 類來管理ProgressBar/ProgressDialog 的顯示/取消,以及更多其它操作。現在,Volley 是替代 AsyncTask 的強大工具。

 

Volley 能做什麼

Volley 適合小而快的資料轉送,特別是針對以下兩種情況:

  • JSON 對象
  • 圖片載入

Volley 優勢:

 

Volley 架構

Volley 使用了線程池來作為基礎結構,主要分為主線程,cache 線程和 network 線程。主線程和 cache 線程都只有一個,而 NetworkDispatcher 線程可以有多個,這樣能解決比並行問題。

圖 2 Volley 架構

 

示範 Volley 庫通過網路擷取 JSON 資料

下載 Demo,將 Volley 庫和示範項目匯入到 eclipse 裡,再將 Volley 庫添加到示範項目。程式運行結果 3 所示:

圖 3 示範 Volley 庫通過網路擷取 JSON 資料

通過 Volley 訪問國家氣象局提供的 JSON 介面,獲得北京的天氣狀況。

  • http://www.weather.com.cn/data/sk/101010100.html
  • http://www.weather.com.cn/data/cityinfo/101010100.html
  • http://m.weather.com.cn/data/101010100.html

其中,101010100 代表北京。

activity_main.xml 檔案只是一個 TextView 控制項,一個 ProgreeBar 控制項,略,核心代碼如下所示:

package com.example.volleybasicexample;
 
import org.json.JSONObject;
 
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
 
public class MainActivity extends Activity {
    private TextView txtDisplay;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        txtDisplay = (TextView) findViewById(R.id.txtDisplay);
 
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "http://www.weather.com.cn/data/sk/101010100.html";
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(
                Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
 
                    @Override
                    public void onResponse(JSONObject response) {
                        // TODO Auto-generated method stub
                        txtDisplay.setText("Response => " + response.toString());
                        findViewById(R.id.progressBar1)
                                .setVisibility(View.GONE);
                    }
                }, new Response.ErrorListener() {
 
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
 
                    }
                });
 
        queue.add(jsObjRequest);
 
    }
}

 

參考資料
  • Android Async HTTP Clients: Volley vs Retrofit
  • Android Volley

 

下載 Demo

聯繫我們

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