目前,当我尝试发出获取请求时,我必须先进行身份验证。所以在网上看,我似乎需要使用 getHeaders() 并使用 HashMap 来提交我的凭据。但是我的标题有一些我需要发送的额外信息。
我不确定是什么问题。我觉得我正在发送所有正确的信息。
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlJsonObj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// JSONObject jsonArray = response.getJSONObject("response_message");
String message = response.getString("response_message");
Toast.makeText(getActivity(),
message,
Toast.LENGTH_LONG).show();
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
) {
@Override
public Map getHeaders() throws AuthFailureError {
Map headers = new HashMap<>();
String credentials = "ezhu:Ccare@123";
String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Content-Type", "application/json");
headers.put("Authorization", auth);
headers.put("user-tz", "-330");
return headers;
}
};
requestQue.add(jsonObjectRequest);
}
我的日志中出现 401 错误,它为我提供了 URL。当我单击该 URL 时,浏览器中的一个选项卡打开并显示以下内容:
{"error_message":"Missing request method parameter","error_code":"1","error":"Missing request header 'user-tz' for method parameter of type String"}
最佳答案
您的代码似乎是正确的。您可以尝试在日志中找到一些东西。当我处理类似问题时,我发现
W/System: Ignoring header sessionID because its value was null.
在我的日志中,什么是很有帮助的
关于android - Volley 认证 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38840615/