Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BasicNetwork.performRequest: Unexpected response code 401 android Volley library

Iam calling web service in android . in that i want to call the URL i am not sending any params to the server, just calling the URL ,

But its getting Error like [10520] BasicNetwork.performRequest: Unexpected response code 401

my code is

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);

How to solve this?

like image 571
John Avatar asked Apr 06 '15 09:04

John


3 Answers

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);
like image 177
mattyU Avatar answered Oct 10 '22 05:10

mattyU


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.

like image 30
Gabe Sechan Avatar answered Oct 10 '22 04:10

Gabe Sechan


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>()
like image 39
Bahu Avatar answered Oct 10 '22 05:10

Bahu