简体   繁体   中英

Android Volley - make POST with body and url parameters - getParams and getBody at the same time

I have simple question which method to call on POST and which method to call on GET.

This is my simple Class:

public class CustomStringRequest extends Request<CustomNetworkResponse> {

private final Map<String, String> headers;
private final Map<String, String> params;
private final String body;
private final Response.Listener<CustomNetworkResponse> listener;


public CustomStringRequest(int method,
                           String url,
                           Map<String, String> headers,
                           Map<String, String> params,
                           String body,
                           Response.Listener<CustomNetworkResponse> listener,
                           Response.ErrorListener errorListener) {

    super(method, url, errorListener);
    this.headers = headers;
    this.params = params;
    this.body = body;
    this.listener = listener;
}



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

@Override
public Map<String, String> getParams() {
    return params;
}

@Override
public byte[] getBody() throws AuthFailureError {
    return body != null ? body.getBytes() : null;
}

How can I use getParams() and getBody() at the same time? Is it possible, because when I check the super implementation I assume it's impossible.

I also commented out the method getBody() to see thats true.

Does that mean that i can't send POST with body and also url parameters? Are url parameters meant to be used for GET request?? Is this standard?

Well I am not sure what your intentions with that! You can always append the url with your url parameters (If the parameters are that simple). And Then You can use getParams() or getBody() to pass more complex parameters.

I think the difference between those two is the encoding of your parameters while they travel through the network (though I am not 100% sure with this) but Yes. getBody() more secure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM