BasicNetwork.performRequest: Unexpected response code 401 android Volley library
Asked Answered
F

6

6

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?

Frequentation answered 6/4, 2015 at 9:39 Comment(0)
R
7

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);
Recipient answered 7/5, 2016 at 18:58 Comment(0)
M
2

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.

Mistletoe answered 6/4, 2015 at 9:42 Comment(2)
i am calling the login web service with json params its working fine after login web service call its not working. any guess???Frequentation
Yes, of those images need authenticationMistletoe
S
1

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>()
Supernumerary answered 16/5, 2016 at 8:2 Comment(0)
H
1
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;
}
Hayden answered 18/12, 2018 at 15:10 Comment(1)
Please add some context to your answer.Quarterstaff
H
1

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;
        }
Harrovian answered 20/1, 2019 at 0:4 Comment(0)
H
1

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

Horodko answered 15/5, 2019 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.