0

I am trying to check if cardinals user provided are valid using get request with Authentication header. To be honest I have not tried success scenario, however when I pass not valid cardinals I get 401 error from server as supposed. However Volley do not handle this error as it suppose to do.

My request:

StringRequest loginRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response) {
                        valid[0] = 1;
                        Log.d("Response", response);
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        valid[0] = 2;
                        Log.d("ERROR","error => "+error.toString());
                    }
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String>  params = new HashMap<String, String>();
                String usernamepwd = u+":"+p;
                params.put("Authentication", Base64.encodeToString(usernamepwd.getBytes(), Base64.DEFAULT));
                return params;
            }
        };

after executing the code I get:

03-02 08:43:32.088 4119-4150/com.example E/Volley: [209] BasicNetwork.performRequest: Unexpected response code 401 for https://example.com/api/checklogin

Overrided function onErrorResponse is not called. What Can I do to properly handle HTTP errors?

  • Looks like Volley doesn't handle 401 errors correctly, see http://stackoverflow.com/questions/22948006/http-status-code-in-android-volley-when-error-networkresponse-is-null/23163279#23163279 for more details – mlidal Mar 02 '17 at 08:19
  • Find more below that logcat line, I think you will find a line with `...com.android.volley.AuthFailureError`, that means `onErrorResponse` called. Or you can set breakpoint inside `onErrorResponse` and start debugging by Shift-F9 to check – BNK Mar 02 '17 at 09:04

0 Answers0