BasicNetwork.performRequest: Unexpected response code 401 android Volley library

孤者浪人 提交于 2019-12-05 15:20:31

This error means you need to authenticate. You can do so adding getHeaders() to your code, so it goes like this:

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 means that the website requires authentication and it was not provided or it failed. You need to authenticate yourself. Unknown whether you need to provide HTTP Basic Authentication or if the webservice has special authentication required and is just being clever with its return value.

If we use POST instead of GET or GET instead of POST mans this error will occur

So, Change GET to Post in this line

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 = 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;
}

Add 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;
        }

Add Headers ... Perhaps you have forget to add headers in volley requests.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!