首先做出整個應用的全局請求隊列
1 package com.qg.lizhanqi.myvolleydemo; 2 3 import android.app.Application; 4 5 import com.android.volley.RequestQueue; 6 import com.android.volley.toolbox.HttpStack; 7 import com.android.volley.toolbox.Volley; 8 /** 9 * Created by lizhanqi on 2016-7-27-0027. 10 */ 11 12 /** 13 * 這是一個本應用全局的Volley請求隊列,所以這里繼承了Application 14 * 由於這是一個自定義的全局的application,所以在清單文件application中加入屬性 15 * android:name=".MyApplication" 16 */ 17 public class MyApplication extends Application { 18 public static RequestQueue queues; 19 20 @Override 21 public void onCreate() { 22 super.onCreate(); 23 //實例化全局的請求隊列 24 queues = Volley.newRequestQueue(getApplicationContext(), (HttpStack) null); 25 } 26 public static RequestQueue getHttpQueues() { 27 return queues; 28 } 29 }
然后是StringRequest的GET請求方式
1 private void volleyGetStringMonth(String url) { 2 /** 3 * StringRequest(int method, String url, Listener<String> listener, ErrorListener errorListener) 4 * method請求方法 url請求路徑,Listener請求成功的監聽的回調,ErrorListener請求失敗的監聽回調 5 */ 6 StringRequest sr = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { 7 @Override 8 public void onResponse(String s) { 9 Log.i(TAG, "onResponse: 成功了"); 10 Toast.makeText(MainActivity.this, "請求成功volleyGetStringMonth" + s, Toast.LENGTH_SHORT).show(); 11 } 12 }, new Response.ErrorListener() { 13 @Override 14 public void onErrorResponse(VolleyError volleyError) { 15 Log.i(TAG, "onErrorResponse: 失敗了"); 16 } 17 }); 18 //設置請求標簽用於加入全局隊列后,方便找到 19 sr.setTag("stringquest"); 20 //添加到全局的請求隊列 21 MyApplication.getHttpQueues().add(sr); 22 }
接着是StringRequest的POST請求方式的使用
1 private void volleyPostStringMonth(String url) { 2 StringRequest postsr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { 3 @Override 4 public void onResponse(String s) { 5 Log.i(TAG, "onResponse: 成功了"); 6 Toast.makeText(MainActivity.this, "volleyPostStringMonth請求成功:" + s, Toast.LENGTH_SHORT).show(); 7 } 8 }, new Response.ErrorListener() { 9 @Override 10 public void onErrorResponse(VolleyError volleyError) { 11 Log.i(TAG, "onErrorResponse: 失敗了"); 12 } 13 }) { 14 @Override 15 protected Map<String, String> getParams() throws AuthFailureError { 16 //創建一個集合,放的是keyvalue的key是參數名與value是參數值 17 Map<String, String> map = new HashMap<String, String>(); 18 map.put("type", "news"); 19 map.put("nums", "20"); 20 return map; 21 } 22 23 24 }; 25 //設置請求標簽用於加入全局隊列后,方便找到 26 postsr.setTag("postsr"); 27 //添加到全局的請求隊列 28 MyApplication.getHttpQueues().add(postsr); 29 }
最后是JsonObjectRequest的簡單使用
1 private void volleyGetJsonMonth(String url) { 2 /* 3 * JsonObjectRequest(int method, String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener) 4 method 請求方式 5 url請求路徑 6 */ 7 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 8 @Override 9 public void onResponse(JSONObject jsonObject) { 10 Log.i(TAG, "onResponse: JsonObjectRequest成功了"); 11 Toast.makeText(MainActivity.this, "volleyGetJsonMonth請求成功:" + jsonObject, Toast.LENGTH_SHORT).show(); 12 } 13 }, new Response.ErrorListener() { 14 @Override 15 public void onErrorResponse(VolleyError volleyError) { 16 Log.i(TAG, "onErrorResponse: JsonObjectRequest失敗了"); 17 } 18 }); 19 jsonObjectRequest.setTag("jsonObjectRequest"); 20 MyApplication.getHttpQueues().add(jsonObjectRequest); 21 }
然后我們還可以重寫onstop不可見后就取消請求
1 @Override 2 protected void onStop() { 3 MyApplication.getHttpQueues().cancelAll("stringquest"); 4 MyApplication.getHttpQueues().cancelAll("jsonObjectRequest"); 5 MyApplication.getHttpQueues().cancelAll("postsr"); 6 super.onStop(); 7 }
