app/src/main/java/com/ar/tdp2fiuba/hoycomo/service/HttpRequestHelper.java
/**
     * Adds to the RequestQueue{@link RequestQueue} the request to perform.
     * @param method is the HTTP Method
     * @param url is the url pointed by the request
     * @param requestParams are the key-value parameters added to the url
     * @param jsonRequest is the JSON body of the request
     * @param successListener is the listener that triggers the actions to perform when the response is successful
     * @param errorListener is the listener that triggers the actions to perform when the response fails
     * @param requestTag the tag associated to the request to enqueue
     * @return the enqueued Request{@link Request} or null{@code null} if the RequestQueue couldn't be initialized.
     */
Adds to the RequestQueue RequestQueue the request to perform.
Adds to the RequestQueue RequestQueue the request to perform .
private static Request<JSONArray> enqueue(int method, String url, @Nullable final Map<String, String> requestParams, JSONArray jsonRequest,
                                               final Response.Listener<JSONArray> successListener, final Response.ErrorListener errorListener, String requestTag) {
        if (mRequestQueue == null)
            return null;
        JsonArrayRequest request = new JsonArrayRequest(method, url, jsonRequest, successListener, errorListener) {
            @Override
            protected Map<String, String> getParams() {
                return requestParams;
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                headers.put("Connection", "close"); 
                headers.put("Authorization", "Bearer " + UserAuthenticationManager.getToken(context));
                return headers;
            }
            @Override
            protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
                try {
                    if (response.data.length == 0) {
                        byte[] responseData = "{}".getBytes("UTF8");
                        response = new NetworkResponse(response.statusCode, responseData, response.headers, response.notModified);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return super.parseNetworkResponse(response);
            }
        };
        request.setTag(requestTag);
        request.setRetryPolicy(new DefaultRetryPolicy((int) TimeUnit.SECONDS.toMillis(3), 2, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        return mRequestQueue.add(request);
    }
private static Request < JSONArray > enqueue ( int method , String url , @ Nullable final Map < String , String > requestParams , JSONArray jsonRequest , final Response . Listener < JSONArray > successListener , final Response . ErrorListener errorListener , String requestTag ) { if ( mRequestQueue == null ) return null ; JsonArrayRequest request = new JsonArrayRequest ( method , url , jsonRequest , successListener , errorListener ) { @ Override protected Map < String , String > getParams ( ) { return requestParams ; } @ Override public Map < String , String > getHeaders ( ) throws AuthFailureError { Map < String , String > headers = new HashMap < > ( ) ; headers . put ( "Connection" , "close" ) ; headers . put ( "Authorization" , "Bearer " + UserAuthenticationManager . getToken ( context ) ) ; return headers ; } @ Override protected Response < JSONArray > parseNetworkResponse ( NetworkResponse response ) { try { if ( response . data . length == 0 ) { byte [ ] responseData = "{}" . getBytes ( "UTF8" ) ; response = new NetworkResponse ( response . statusCode , responseData , response . headers , response . notModified ) ; } } catch ( UnsupportedEncodingException e ) { e . printStackTrace ( ) ; } return super . parseNetworkResponse ( response ) ; } } ; request . setTag ( requestTag ) ; request . setRetryPolicy ( new DefaultRetryPolicy ( ( int ) TimeUnit . SECONDS . toMillis ( 3 ) , 2 , DefaultRetryPolicy . DEFAULT_BACKOFF_MULT ) ) ; return mRequestQueue . add ( request ) ; }
Adds to the RequestQueue{@link RequestQueue} the request to perform.
Adds to the RequestQueue { @link RequestQueue } the request to perform .
// Créditos a Martín Cura <3