UnsupportedOperationException while calling Volley request with headers
Asked Answered
S

2

3

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


    }
Sommers answered 20/4, 2016 at 16:47 Comment(2)
in what line you have "UnsupportedOperationException"Dasie
Remove if(params==null)Quarrier
G
23

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.

Gerson answered 20/5, 2016 at 10:33 Comment(0)
B
0

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.

Biscuit answered 12/10, 2020 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.