4

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I 'll confirm that the server system to which my app sends requests can accept array parameters with explicit index numbers like [0], [1] ... [n-1].
2

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.

1 Comment

Thanks for your answer. I see. Using getBody(), we can make query string as body in more raw level instead of Map. So, in that method, I'll try to build String like "x[]=10&x[]=20&x[]=30" and return byte[] of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.