Skip to content

Commit 718f01c

Browse files
committed
Merge pull request android-async-http#68 from joebroni/master
Allow more than one Request param with the same key.
2 parents c0a31d3 + 04a14d0 commit 718f01c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

local.properties.dist

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.FileInputStream;
2424
import java.io.FileNotFoundException;
2525
import java.io.UnsupportedEncodingException;
26+
import java.util.ArrayList;
2627
import java.util.LinkedList;
2728
import java.util.List;
2829
import java.util.Map;
@@ -57,6 +58,7 @@ public class RequestParams {
5758

5859
protected ConcurrentHashMap<String, String> urlParams;
5960
protected ConcurrentHashMap<String, FileWrapper> fileParams;
61+
protected ConcurrentHashMap<String, ArrayList<String>> urlParamsWithArray;
6062

6163
/**
6264
* Constructs a new empty <code>RequestParams</code> instance.
@@ -129,6 +131,17 @@ public void put(String key, File file) throws FileNotFoundException {
129131
put(key, new FileInputStream(file), file.getName());
130132
}
131133

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+
132145
/**
133146
* Adds an input stream to the request.
134147
* @param key the key name for the new param.
@@ -168,6 +181,7 @@ public void put(String key, InputStream stream, String fileName, String contentT
168181
public void remove(String key){
169182
urlParams.remove(key);
170183
fileParams.remove(key);
184+
urlParamsWithArray.remove(key);
171185
}
172186

173187
@Override
@@ -191,6 +205,20 @@ public String toString() {
191205
result.append("FILE");
192206
}
193207

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+
194222
return result.toString();
195223
}
196224

@@ -224,6 +252,14 @@ public HttpEntity getEntity() {
224252
currentIndex++;
225253
}
226254

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+
227263
entity = multipartEntity;
228264
} else {
229265
try {
@@ -239,6 +275,7 @@ public HttpEntity getEntity() {
239275
private void init(){
240276
urlParams = new ConcurrentHashMap<String, String>();
241277
fileParams = new ConcurrentHashMap<String, FileWrapper>();
278+
urlParamsWithArray = new ConcurrentHashMap<String, ArrayList<String>>();
242279
}
243280

244281
protected List<BasicNameValuePair> getParamsList() {
@@ -248,6 +285,13 @@ protected List<BasicNameValuePair> getParamsList() {
248285
lparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
249286
}
250287

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+
251295
return lparams;
252296
}
253297

0 commit comments

Comments
 (0)