e.k
e.k

Reputation: 1363

How to make volley post string request with attaching headers?

I google and I tried to post string request by attaching headers. But I always get auth failure error.It seems my header is not setting properly. Here is my code

StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("response",response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (error!=null){
                            error.printStackTrace();
                        }
                    }
                }){

            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put("client_id=",getString(R.string.client_id));
                params.put("&client_secret=",getString(R.string.client_secret));
                params.put("&grant_type=","authorization_code");
                params.put("&code=",accessToken);
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("Authorization", "Basic " + base64);
                params.put("Content-Type", "application/x-www-form-urlencoded");
                params.put("Accept", "*/*" );
                return super.getHeaders();
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(stringRequest);

I attached headers in getHeaders() and stream writer in getParams().Can somebody help me to resolve this.Thanks in advance.

Upvotes: 0

Views: 2440

Answers (1)

Sohail Zahid
Sohail Zahid

Reputation: 8149

Dont put params like this

params.put("&grant_type=","authorization_code");

replace like this

params.put("grant_type", authorization_code);

Dont add & and = sign it will automatically included.

 @Override
 protected Map<String,String> getParams(){
    Map<String,String> params = new HashMap<String, String>();
    params.put("client_id",getString(R.string.client_id));
                params.put("client_secret",getString(R.string.client_secret));
                params.put("grant_type","authorization_code");
                params.put("code",accessToken);
                return params;
            }

try header like this

@Override
 public Map getHeaders() throws AuthFailureError {
   Map headers = new HashMap();
   headers.put("appId", "MYAPP_ID_HERE");
   return headers;
 }

Upvotes: 1

Related Questions