Add custom headers with the Volley library

I am starting to use Volley for my application, and I want to add my own headers for each request as a security identifier. I use JsonObjectRequestand redefine getHeaders().

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
                        url,
                        null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                Log.d(TAG, response.toString());
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Log.d(TAG, error.toString());
                            }
                        }) {
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<>();
                        String mApiKey = "123";
                        headers.put("APIKEY", mApiKey);
                        return headers;
                    }

                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<>();
                        params.put("param1", "1");
                        params.put("param2", "2");
                        params.put("param3", "3");
                        return params;
                    }
                };
                VolleySingleton.getInstance(getActivity()).addToRequestQueue(jsonObjectRequest);

But I get this error:

E/Volley﹕ [23620] BasicNetwork.performRequest: Unexpected response code 401 for http://...

Thrown out AuthFailureError.

I am also trying to use StringRequest, but the same error.

If someone is in the same case and has a solution, thanks in advance!

+4
source share
2 answers

This is the basic concept of overriding the header in the standard VolleyRequest file.

VolleyRequest networkRequest = new VolleyRequest(request.getHttpMethod(), mUrlBase + request.getUrlSuffix(), responseListener, errorListener) {
        public String getBodyContentType() {
            return "application/json; charset=" + getParamsEncoding();
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {               
             HashMap<String, String> map = new HashMap<String, String>();
            map.put("X-Device-Info","Android FOO BAR");

            map.put("Accept-Language", acceptLanguage);
            map.put("Content-Type", "application/json; charset=UTF-8");         

            return map;             
        }

        public byte[] getBody() throws AuthFailureError {

            try {
                String json = request.toJson().toString();
                if (json.length() < 3)
                    return ("{}").getBytes();
                // log(json);
                return json.getBytes(getParamsEncoding());
            } catch (UnsupportedEncodingException e) {
                Log.e(TAG, "getBody(): request has no json");
                e.printStackTrace();
            }
            return new byte[0];
        }
    };
+3
source
public class CustomJsonObjectRequest  extends JsonObjectRequest
{
    public CustomJsonObjectRequest(int method, String url, JSONObject jsonRequest,Response.Listener listener, Response.ErrorListener errorListener)
    {
        super(method, url, jsonRequest, listener, errorListener);
    }


@Override
public Map getHeaders() throws AuthFailureError {
    Map headers = new HashMap();
    headers.put(Constants.accesstoken, Globals.getInstance().getAccessToken());
    Logger.debugE(Constants.accesstoken, headers.toString());
    return headers;
    }

}
0
source

All Articles