How to pass parameters in a request with Volley

Asked

Viewed 288 times

0

Gentlemen I created a Custom class for the Request with Volley knowing that this form is possible to send parameters however I see that this is not what happens what can be wrong then?

The code is no different than 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 follows below but 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));

1 answer

0

The Volley library does not work with parameters for all requests only for POST and PUT, even examples where customizations are made if you pass the GET method will no longer work if in the customization you must override the 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;
}

Observation, original solution
in posted by Andrea Motto

Browser other questions tagged

You are not signed in. Login or sign up in order to post.