23
23
import java .io .FileInputStream ;
24
24
import java .io .FileNotFoundException ;
25
25
import java .io .UnsupportedEncodingException ;
26
+ import java .util .ArrayList ;
26
27
import java .util .LinkedList ;
27
28
import java .util .List ;
28
29
import java .util .Map ;
@@ -57,6 +58,7 @@ public class RequestParams {
57
58
58
59
protected ConcurrentHashMap <String , String > urlParams ;
59
60
protected ConcurrentHashMap <String , FileWrapper > fileParams ;
61
+ protected ConcurrentHashMap <String , ArrayList <String >> urlParamsWithArray ;
60
62
61
63
/**
62
64
* Constructs a new empty <code>RequestParams</code> instance.
@@ -129,6 +131,17 @@ public void put(String key, File file) throws FileNotFoundException {
129
131
put (key , new FileInputStream (file ), file .getName ());
130
132
}
131
133
134
+ /**
135
+ * Adds param with more than one value.
136
+ * @param key the key name for the new param.
137
+ * @param values is the ArrayList with values for the param.
138
+ */
139
+ public void put (String key , ArrayList <String > values ) {
140
+ if (key != null && values != null ) {
141
+ urlParamsWithArray .put (key , values );
142
+ }
143
+ }
144
+
132
145
/**
133
146
* Adds an input stream to the request.
134
147
* @param key the key name for the new param.
@@ -168,6 +181,7 @@ public void put(String key, InputStream stream, String fileName, String contentT
168
181
public void remove (String key ){
169
182
urlParams .remove (key );
170
183
fileParams .remove (key );
184
+ urlParamsWithArray .remove (key );
171
185
}
172
186
173
187
@ Override
@@ -191,6 +205,20 @@ public String toString() {
191
205
result .append ("FILE" );
192
206
}
193
207
208
+ for (ConcurrentHashMap .Entry <String , ArrayList <String >> entry : urlParamsWithArray .entrySet ()) {
209
+ if (result .length () > 0 )
210
+ result .append ("&" );
211
+
212
+ ArrayList <String > values = entry .getValue ();
213
+ for (String value : values ) {
214
+ if (values .indexOf (value ) != 0 )
215
+ result .append ("&" );
216
+ result .append (entry .getKey ());
217
+ result .append ("=" );
218
+ result .append (value );
219
+ }
220
+ }
221
+
194
222
return result .toString ();
195
223
}
196
224
@@ -224,6 +252,14 @@ public HttpEntity getEntity() {
224
252
currentIndex ++;
225
253
}
226
254
255
+ // Add dupe params
256
+ for (ConcurrentHashMap .Entry <String , ArrayList <String >> entry : urlParamsWithArray .entrySet ()) {
257
+ ArrayList <String > values = entry .getValue ();
258
+ for (String value : values ) {
259
+ multipartEntity .addPart (entry .getKey (), value );
260
+ }
261
+ }
262
+
227
263
entity = multipartEntity ;
228
264
} else {
229
265
try {
@@ -239,6 +275,7 @@ public HttpEntity getEntity() {
239
275
private void init (){
240
276
urlParams = new ConcurrentHashMap <String , String >();
241
277
fileParams = new ConcurrentHashMap <String , FileWrapper >();
278
+ urlParamsWithArray = new ConcurrentHashMap <String , ArrayList <String >>();
242
279
}
243
280
244
281
protected List <BasicNameValuePair > getParamsList () {
@@ -248,6 +285,13 @@ protected List<BasicNameValuePair> getParamsList() {
248
285
lparams .add (new BasicNameValuePair (entry .getKey (), entry .getValue ()));
249
286
}
250
287
288
+ for (ConcurrentHashMap .Entry <String , ArrayList <String >> entry : urlParamsWithArray .entrySet ()) {
289
+ ArrayList <String > values = entry .getValue ();
290
+ for (String value : values ) {
291
+ lparams .add (new BasicNameValuePair (entry .getKey (), value ));
292
+ }
293
+ }
294
+
251
295
return lparams ;
252
296
}
253
297
0 commit comments