我开始在我的应用中使用Volley,并希望添加自定义头以作为安全标识符。我正在使用JsonObjectRequest
并覆盖getHeaders()
方法。
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
String mApiKey = "123";
headers.put("APIKEY", mApiKey);
return headers;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("param1", "1");
params.put("param2", "2");
params.put("param3", "3");
return params;
}
};
VolleySingleton.getInstance(getActivity()).addToRequestQueue(jsonObjectRequest);
但是我遇到了这个错误:
E/Volley﹕ [23620] BasicNetwork.performRequest: Unexpected response code 401 for http://...
抛出
AuthFailureError
错误。
我也尝试使用StringRequest
,但是出现了相同的错误。
如果有人遇到相同情况并且有解决方案,请提前感谢!