Agregar encabezados personalizados con la biblioteca Volley

Comienzo a usar Volley para mi aplicación y quiero agregar encabezados personalizados para cada solicitud como un identificador de seguridad. Estoy usando un JsonObjectRequest y reemplazando el 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); 

Pero tengo este error:

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

El AuthFailureError se AuthFailureError .

También intento utilizar StringRequest pero el mismo error.

Si alguien está en el mismo caso y tiene solución, gracias de antemano!

Este es un concepto básico de cómo anular un encabezado en un VolleyRequest estándar

 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]; } }; 
 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; } } 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.