简体   繁体   English

如何通过com.android.volley.Request发送数组类型参数

[英]How to send array type parameters via com.android.volley.Request

Now, I'm making an Android app and I have a question about this title. 现在,我正在制作一个Android应用程序,我对这个标题有疑问。

I use com.android.volley APIs to send query parameters.To send parameters, I made a class extending com.android.volley.Request and intended to oderride the method of this: 我使用com.android.volley API来发送查询参数。为了发送参数,我做了一个扩展com.android.volley.Request的类,并打算用oderride这个方法:

protected Map getParams() throws AuthFailureError protected Map getParams()抛出AuthFailureError

But afterwards, I needed to send array type parameters like 但之后,我需要发送数组类型参数,如

x[]=10&x[]=20&x[]=30 X [] = 10&X [] = 20&X [] = 30

But I can't send these array type parameters by above method getParams() . 但是我不能通过上面的方法getParams()发送这些数组类型参数。 Because a map can have only one value for the key of String "x[]". 因为一个映射只能有一个String“x []”键的值。

Please give me some advices for how to send array type parameters via com.android.volley.Request . 请给我一些关于如何通过com.android.volley.Request发送数组类型参数的建议。

Regards. 问候。

I think i can better explain you with the Code.Suppose you need to send an array of n elements.Then you should send it like. 我想我可以用Code.Sseppose更好地解释你需要发送n个元素的数组。然后你应该发送它。

params.put("x[0]","value1");
params.put("x[1]","value2");
   .
   .
   .
params.put("x[n-1]","valuen");

returns params from getParams method you are done.Hope it will help.Thanks. 从getParams方法返回params你完成了。希望它会有所帮助。谢谢。

If we look at the request class we can find below functions: 如果我们查看请求类,我们可以找到以下函数:

/**
 * Returns the raw POST or PUT body to be sent.
 *
 * @throws AuthFailureError in the event of auth failure
 */
public byte[] getBody() throws AuthFailureError {
    Map<String, String> params = getParams();
    if (params != null && params.size() > 0) {
        return encodeParameters(params, getParamsEncoding());
    }
    return null;
}

/**
 * Converts <code>params</code> into an application/x-www-form-urlencoded encoded string.
 */
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
    StringBuilder encodedParams = new StringBuilder();
    try {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
            encodedParams.append('=');
            encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
            encodedParams.append('&');
        }
        return encodedParams.toString().getBytes(paramsEncoding);
    } catch (UnsupportedEncodingException uee) {
        throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
    }
}

So you can override byte[] getBody() throws AuthFailureError . 所以你可以覆盖byte[] getBody() throws AuthFailureError Create your own body and convert it to byte[] and return it. 创建自己的主体并将其转换为byte[]并返回它。

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

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