Bartos
Bartos

Reputation: 1037

UnsupportedOperationException while calling Volley request with headers

I try to make call request with Volley library. I want to set headers, but I'm getting 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);


    }

Upvotes: 3

Views: 3163

Answers (2)

user1557803
user1557803

Reputation: 39

Use the below method to solve UnsupportedOperationException:

override fun getHeaders(): MutableMap<String, String> {
    val header = HashMap<String, String>()
//  header[Constants.AUTH_TOKEN] = Constants.TOKEN_ID
    return header
}

This is working for me.

Upvotes: 0

stevyhacker
stevyhacker

Reputation: 1877

Remove these two lines:

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

And add just this:

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

It should work now.

Upvotes: 23

Related Questions