標籤:架構 android 程式碼片段 json cache
Volley 下載
- git clone https://android.googlesource.com/platform/frameworks/volley
下載之後引入項目即可 ,volley 適合資料小但是連續的通訊,,至於下載檔案就支援的不是很好,建議用android 內建的downLoadmanager ,.android 連網需要的網路許可權,和對儲存卡的讀寫權限是不能缺少的。。。 Volley 中用的最多的也就Volley.class,StringRequest,JsonRequest,ImageRequest.當然你也可以自己實現。繼承Request<T>即可。。一般在項目中開發還需要自訂Volley.class 。例如 自訂的on-disk cache directory , userAgent, 可以修改volley原項目的檔案,或者自訂MyVolley。兩種方法自己選擇。。擷取百度首頁的源碼
StringRequest request = new StringRequest(host, new Listener<String>() {@Overridepublic void onResponse(String response) {Logg.logg("請求成功運行 ");// 輸出結果 。。。。。Logg.logg(response);}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) { Logg.logg("請求錯誤運行 ");}});queue.add(request);queue.start() ;擷取json
JsonObjectRequest objectRequest = new JsonObjectRequest(json, null, new Response.Listener<JSONObject>() {@Overridepublic void onResponse(JSONObject response) { Logg.logg(response.toString());}}, null);queue.add(objectRequest);queue.start() ;從網路載入圖片不要用imageview ,而要用NetWorkImageView .. 並且要實現imagecache ..建議 .
post請求 需要重寫StringRequest 中的getParam 方法,, demo 。。http://download.csdn.net/detail/u014781439/8324529
android http 架構volley 的學習 。。