UnsupportedOperationException when calling a Volley request with headers

I am trying to execute a call request using the Volley library. I want to set headers, but I get java.lang.UnsupportedOperationException. Do you know why, and how can I solve this problem?

 public void getAccountInfo() {
            RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
            String url = "http://demo.gopos.pl/oauth/token";
            StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            // Display the first 500 characters of the response string.
                            Log.e(TAG, "onResponse SUCCES!!" + response);

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "That didn't work!");
            }

        }) {
            @Override
            public Map<String,String> getHeaders()throws AuthFailureError {
                Map<String,String> params = super.getHeaders();
                if(params==null)params = new HashMap<>();
                params.put("username","username");
                params.put("password","password");
                return params;
            }
        };
        queue.add(stringRequest);


    }
+3
source share
1 answer

Delete these two lines:

 Map<String,String> params = super.getHeaders();
            if(params==null)params = new HashMap<>();

And add only this:

   Map<String, String> params = new HashMap<>();

Now it should work.

+18
source

Source: https://habr.com/ru/post/1611091/


All Articles