不带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);
本文详细介绍了如何使用Volley库进行网络请求,包括不带TOKEN和带TOKEN的两种请求方式。通过具体的Java代码示例,展示了如何创建请求队列、发起JSON对象请求,并设置响应监听器及错误监听器。
2317

被折叠的 条评论
为什么被折叠?



