Skip to content

Instantly share code, notes, and snippets.

@hidroh
Created March 8, 2015 03:48
Show Gist options
  • Save hidroh/0a6d24dc32dd640df045 to your computer and use it in GitHub Desktop.
Save hidroh/0a6d24dc32dd640df045 to your computer and use it in GitHub Desktop.
Volley request class with support for array values in payload
import com.android.volley.AuthFailureError;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
* Volley request class with support for array values in payload
*/
public class Request<T> extends com.android.volley.Request<T> {
private static final String ARRAY_PARAM = "%s[]";
/**
* Returns custom body payload with support for array params
* If not empty, this payload will be used in favor to one returned by {@link #getParams()}
* @return an instance of {@link Request.RequestParams}
* @see #getParams()
* @see #getBody()
*/
protected RequestParams getPayload() {
return null;
}
@Override
public byte[] getBody() throws AuthFailureError {
// if no custom payload specified, we fallback to default getBody()
RequestParams payload = getPayload();
if (payload == null || payload.isEmpty()) {
return super.getBody();
}
StringBuilder encodedParams = new StringBuilder();
String paramsEncoding = getParamsEncoding();
try {
Map<String, String> stringParams = payload.stringParams;
for (Map.Entry<String, String> entry : stringParams.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
Map<String, Iterable<String>> arrayParams = payload.arrayParams;
for (Map.Entry<String, Iterable<String>> entry : arrayParams.entrySet()) {
for (String value : entry.getValue()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(value, paramsEncoding));
encodedParams.append('&');
}
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
/**
* Custom payload with support for array params
*/
public static class RequestParams {
private final Map<String, String> stringParams = new HashMap<>();
private final Map<String, Iterable<String>> arrayParams = new HashMap<>();
/**
* Set string param
* @param key param key
* @param value param value
*/
public void put(String key, String value) {
stringParams.put(key, value);
}
/**
* Set array param
* @param key param key
* @param values param values
*/
public void put (String key, Iterable<String> values) {
arrayParams.put(String.format(ARRAY_PARAM, key), values);
}
private boolean isEmpty() {
return stringParams.isEmpty() && arrayParams.isEmpty();
}
}
}
@pong007
Copy link

pong007 commented May 31, 2016

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment