Skip to content

Commit d179fa3

Browse files
committed
Add varargs RequestParams convenience constructor
1 parent 5a336aa commit d179fa3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/com/loopj/android/http/RequestParams.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ public RequestParams(String key, String value) {
9090
put(key, value);
9191
}
9292

93+
/**
94+
* Constructs a new RequestParams instance and populate it with multiple
95+
* initial key/value string param.
96+
* @param keysAndValues a sequence of keys and values. Objects are
97+
* automatically converted to Strings (including the value {@code null}).
98+
* @throws IllegalArgumentException if the number of arguments isn't even.
99+
*/
100+
public RequestParams(Object... keysAndValues) {
101+
int len = keysAndValues.length;
102+
if (len % 2 != 0)
103+
throw new IllegalArgumentException("Supplied arguments must be even");
104+
for (int i = 0; i < len; i += 2) {
105+
String key = String.valueOf(keysAndValues[i]);
106+
String val = String.valueOf(keysAndValues[i + 1]);
107+
put(key, val);
108+
}
109+
}
110+
93111
/**
94112
* Adds a key/value string pair to the request.
95113
* @param key the key name for the new param.

0 commit comments

Comments
 (0)