本文介绍了如何使用Volley库在Android中发送Authorization标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用Volley库中的 GET
方法发送 Authorization
标头?
How can I send Authorization
header using Volley library in Android for GET
method?
这是我的请求代码:
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url,
null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Response", response.toString());
pd.dismiss();
Toast.makeText(MainActivity.this, "" + response.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error", "Error: " + error.getMessage());
Toast.makeText(MainActivity.this, "" + error.getMessage(), Toast.LENGTH_SHORT).show();
pd.dismiss();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "2e96e0a4ff05ba86dc8f778ac49a8dc0");
return headers;
}
};
推荐答案
StringRequest request = new StringRequest(Request.Method.POST, YourUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (!response.equals(null)) {
Log.e("Your Array Response", response);
} else {
Log.e("Your Array Response", "Data Null");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error is ", "" + error);
}
}) {
//This is for Headers If You Needed
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json; charset=UTF-8");
params.put("token", ACCESS_TOKEN);
return params;
}
//Pass Your Parameters here
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("User", UserName);
params.put("Pass", PassWord);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(request);
这篇关于如何使用Volley库在Android中发送Authorization标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!