不带TOKEN值的
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("", "");
} catch (JSONException e) {
e.printStackTrace();
}
RequestQueue requestQueue = Volley.newRequestQueue();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.类型, url, jsonObject,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
requestQueue.add(jsonObjectRequest);
带TOKNE值得
需要提前准备一个工具类MyToken
public class MyToken extends JsonObjectRequest {
public MyToken(int method, String url, @Nullable JSONObject jsonRequest, Response.Listener<JSONObject> listener, @Nullable Response.ErrorListener errorListener) {
super(method, url, jsonRequest, listener, errorListener);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
AppConfig appConfig = new AppConfig();
String token = appConfig.getTOKEN();
Map<String,String> map = new HashMap<>();
map.put("Authorization",token);
return map;
}
}
进行带Token值得Volley请求
JSONObject jsonObject = new JSONObject();
RequestQueue requestQueue = Volley.newRequestQueue(Context);
MyToken myToken = new MyToken(Request.Method.类型, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(myToken);