智慧并不产生于学历,而是来自对于知识的终生不懈的追求。这篇文章主要讲述BasicNetwork.performRequest:意外响应代码401 android Volley库相关的知识,希望能为你提供帮助。
我在android中调用web服务。因为我想调用URL我没有向服务器发送任何参数,只是调用URL,但它的错误如[10520] BasicNetwork.performRequest:意外的响应代码401
【BasicNetwork.performRequest(意外响应代码401 android Volley库)】我的代码是
RequestQueue queue = Volley.newRequestQueue(getActivity());
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, Server.URL, null,
new Response.Listener<
JSONObject>
()
{
@Override
public void onResponse(JSONObject response) {
// display response
hideProgressDialog();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
hideProgressDialog();
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
怎么解决这个?
答案此错误意味着您需要进行身份验证。您可以这样做,将getHeaders()添加到您的代码中,所以它是这样的:
RequestQueue queue = Volley.newRequestQueue(getActivity());
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, Server.URL, null,
new Response.Listener<
JSONObject>
()
{
@Override
public void onResponse(JSONObject response) {
// display response
hideProgressDialog();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
hideProgressDialog();
}
}@Override
public Map<
String, String>
getHeaders() throws AuthFailureError {
HashMap<
String, String>
params = new HashMap<
String, String>
();
params.put("Content-Type", "application/json");
String creds = String.format("%s:%s","username","password");
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
params.put("Authorization", auth);
return params;
}
);
queue.add(getRequest);
另一答案HTTP 401表示网站需要身份验证,但未提供或失败。您需要对自己进行身份验证。未知是否需要提供HTTP基本身份验证,或者Web服务是否需要特殊身份验证,并且只是对其返回值很聪明。
另一答案如果我们使用POST而不是GET或GET而不是POST勒芒,则会发生此错误
所以,在这一行中将GET改为Post
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, Server.URL, null, new Response.Listener<
JSONObject>
()
另一答案
String http_post() {RequestQueue MyRequestQueue = Volley.newRequestQueue(Library.this);
//String url = "http://" + "hqplayer" + ":" + Utils.password + "@" + Utils.ip + ":8088/library";
String url = "http://" + Utils.ip + ":8088/library";
Log.i(TAG, "HttpPost() <
" + url + ">
");
String credentials = "hqplayer:valvole";
byte[] t = credentials.getBytes();
byte[] auth = Base64.encode(t, Base64.DEFAULT);
final String basicAuthValue = https://www.songbingjia.com/android/new String(auth);
MyRequestQueue.add(new StringRequest(Request.Method.POST, url, new Response.Listener<
String>
() {
@Override
public void onResponse(String response) {
Log.i(TAG,"HttpPost() - onResponse() ");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, "HttpPost() - onErrorResponse() ");
Log.i(TAG, "HttpPost() error <
" + error + ">
");
}
}) {
@Override
public Map<
String, String>
getHeaders() throws AuthFailureError {
HashMap<
String, String>
params = new HashMap<
String, String>
();
String creds = String.format("%s:%s","hqplayer","valvole");
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
params.put("Authorization", auth);
return params;
}});
return null;
}
另一答案添加getHeader
@Override
public Map<
String, String>
getHeaders() throws AuthFailureError {
HashMap<
String, String>
headers = new HashMap<
String, String>
();
headers.put("token", SharedVariables.TOKEN);
headers.put("device", SharedVariables.DEVICE_ID);
headers.put("accept-language", "en-US");
headers.put("api-version", "1.0");
return headers;
}
另一答案添加标题...也许您忘记在排球请求中添加标题。
推荐阅读
- 我如何使用“sdk / color.xml”中“app / color.xml”中提到的颜色()
- 在React Native中创建android本机模块时,“undefined不是函数”
- 在从Golang Buffalo webapp发送推文时设置CSRF令牌时遇到问题
- 如何在Android应用程序中一次更改所有活动转换()
- UX研究技术及其应用
- UX研究方法和用户共鸣之路
- 想想S.M.A.R.T.在为下一个UX项目定义业务目标时
- UX产品组合技巧和最佳实践
- 改变是好是坏( UX创新指南)