简体   繁体   中英

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.

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:

protected Map getParams() throws AuthFailureError

But afterwards, I needed to send array type parameters like

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

But I can't send these array type parameters by above method getParams() . Because a map can have only one value for the key of String "x[]".

Please give me some advices for how to send array type parameters via 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.

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.

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 . Create your own body and convert it to byte[] and return it.

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