Created
March 8, 2015 03:48
-
-
Save hidroh/0a6d24dc32dd640df045 to your computer and use it in GitHub Desktop.
Volley request class with support for array values in payload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you