How to pass parameters in a request with Volley

0

Gentlemen, I created a custom class for the Volley Requisition, knowing that this form is possible to send parameters, but I see that this is not what happens, what could be wrong then?

The code is nothing different from what we found on the network

public class CustomRequest extends Request<JSONObject> {
    private Gson mGson = new Gson();
    private Listener<JSONObject> listener;
    private Map<String, String> params;
    private Map<String, String> headers;

    public CustomRequest(String url, Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(Request.Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
        this.headers = null;
        mGson = new Gson();
    }

    public CustomRequest(int method, String url, Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
        this.headers = null;
        mGson = new Gson();
    }

    @Override
    public Map<String, String> getHeaders()
            throws AuthFailureError {
        return headers != null ? headers : super.getHeaders();
    }

    protected Map<String, String> getParams() throws AuthFailureError {
        return params;
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected void deliverResponse(JSONObject response) {

        this.listener.onResponse(response);
    }
}

to consume below below the parameters do not follow in the request

Map<String, String> params = new HashMap<String,String>();
params.put(Helper.TOKEN, "1234567890abcd");

CustomRequest serverRequest = new CustomRequest(
        Helper.PATH_TO_TIPOS ,
        params,
        createRequestSuccessListener(),
        createRequestErrorListener());

serverRequest.setRetryPolicy(new DefaultRetryPolicy(
        Helper.MY_SOCKET_TIMEOUT_MS,
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
asked by anonymous 16.08.2017 / 16:57

1 answer

0

The volley library does not work with parameters for all requests only for POST and PUT, even examples where the customizations are made if passing the GET method will not work anymore if in customization you should override getUrl as below

// Novos atributos 
private int mMethod;
private String mUrl;

@Override
public String getUrl() {
    String url = mUrl;

    if(mMethod == Request.Method.GET) {
        if(params != null) {
            StringBuilder stringBuilder = new StringBuilder(mUrl);
            //mUrl = stringBuilder.toString();
            int i = 1;
            for (Map.Entry<String,String> entry: params.entrySet()) {
                String key;
                String value;
                try {
                    key = URLEncoder.encode(entry.getKey(), "UTF-8");
                    value = URLEncoder.encode(entry.getValue(), "UTF-8");
                    if(i == 1) {
                        stringBuilder.append("?" + key + "=" + value);
                    } else {
                        stringBuilder.append("&" + key + "=" + value);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                i++;
            }

            url = stringBuilder.toString();
        }
    }

    return url;
}

Note, original solution
at posted by Andrea Motto

    
16.08.2017 / 23:09