简体   繁体   English

Android Volley-使用body和url参数进行POST-同时获取getParams和getBody

[英]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. 我有一个简单的问题,即在POST上调用哪种方法,在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? 如何同时使用getParams()和getBody()? 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. 我还注释掉了getBody()方法,以查看多数民众赞成在哪。

Does that mean that i can't send POST with body and also url parameters? 这是否意味着我无法发送带有body和url参数的POST? Are url parameters meant to be used for GET request?? url参数是否打算用于GET请求? 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). 您始终可以在url后面附加url参数(如果参数很简单)。 And Then You can use getParams() or getBody() to pass more complex parameters. 然后,您可以使用getParams()getBody()传递更复杂的参数。

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. 我认为这两者之间的区别是您的参数在通过网络传播时的编码(尽管我对此不是100%肯定),但是可以。 getBody() more secure. getBody()更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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