18
18
19
19
package com .loopj .android .http ;
20
20
21
- import java .io .InputStream ;
22
21
import java .io .File ;
23
22
import java .io .FileInputStream ;
24
23
import java .io .FileNotFoundException ;
24
+ import java .io .InputStream ;
25
25
import java .io .UnsupportedEncodingException ;
26
+ import java .util .ArrayList ;
27
+ import java .util .Collections ;
26
28
import java .util .LinkedList ;
27
29
import java .util .List ;
28
30
import java .util .Map ;
41
43
* <p>
42
44
* <pre>
43
45
* RequestParams params = new RequestParams();
44
- * params.put ("username", "james");
45
- * params.put ("password", "123456");
46
- * params.put ("email", "my@email.com");
46
+ * params.add ("username", "james");
47
+ * params.add ("password", "123456");
48
+ * params.add ("email", "my@email.com");
47
49
* params.put("profile_picture", new File("pic.jpg")); // Upload a File
48
50
* params.put("profile_picture2", someInputStream); // Upload an InputStream
49
51
* params.put("profile_picture3", new ByteArrayInputStream(someBytes)); // Upload some bytes
55
57
public class RequestParams {
56
58
private static String ENCODING = "UTF-8" ;
57
59
58
- protected ConcurrentHashMap < String , String > urlParams ;
60
+ protected List < BasicNameValuePair > urlParams ;
59
61
protected ConcurrentHashMap <String , FileWrapper > fileParams ;
60
62
61
63
/**
@@ -74,7 +76,7 @@ public RequestParams(Map<String, String> source) {
74
76
init ();
75
77
76
78
for (Map .Entry <String , String > entry : source .entrySet ()) {
77
- put (entry .getKey (), entry .getValue ());
79
+ add (entry .getKey (), entry .getValue ());
78
80
}
79
81
}
80
82
@@ -87,7 +89,7 @@ public RequestParams(Map<String, String> source) {
87
89
public RequestParams (String key , String value ) {
88
90
init ();
89
91
90
- put (key , value );
92
+ add (key , value );
91
93
}
92
94
93
95
/**
@@ -105,7 +107,7 @@ public RequestParams(Object... keysAndValues) {
105
107
for (int i = 0 ; i < len ; i += 2 ) {
106
108
String key = String .valueOf (keysAndValues [i ]);
107
109
String val = String .valueOf (keysAndValues [i + 1 ]);
108
- put (key , val );
110
+ add (key , val );
109
111
}
110
112
}
111
113
@@ -114,9 +116,9 @@ public RequestParams(Object... keysAndValues) {
114
116
* @param key the key name for the new param.
115
117
* @param value the value string for the new param.
116
118
*/
117
- public void put (String key , String value ){
119
+ public void add (String key , String value ){
118
120
if (key != null && value != null ) {
119
- urlParams .put ( key , value );
121
+ urlParams .add ( new BasicNameValuePair ( key , value ) );
120
122
}
121
123
}
122
124
@@ -173,13 +175,13 @@ public void remove(String key){
173
175
@ Override
174
176
public String toString () {
175
177
StringBuilder result = new StringBuilder ();
176
- for (ConcurrentHashMap . Entry < String , String > entry : urlParams . entrySet () ) {
178
+ for (BasicNameValuePair pair : urlParams ) {
177
179
if (result .length () > 0 )
178
180
result .append ("&" );
179
181
180
- result .append (entry . getKey ());
182
+ result .append (pair . getName ());
181
183
result .append ("=" );
182
- result .append (entry .getValue ());
184
+ result .append (pair .getValue ());
183
185
}
184
186
185
187
for (ConcurrentHashMap .Entry <String , FileWrapper > entry : fileParams .entrySet ()) {
@@ -204,8 +206,8 @@ public HttpEntity getEntity() {
204
206
SimpleMultipartEntity multipartEntity = new SimpleMultipartEntity ();
205
207
206
208
// Add string params
207
- for (ConcurrentHashMap . Entry < String , String > entry : urlParams . entrySet () ) {
208
- multipartEntity .addPart (entry . getKey (), entry .getValue ());
209
+ for (BasicNameValuePair pair : urlParams ) {
210
+ multipartEntity .addPart (pair . getName (), pair .getValue ());
209
211
}
210
212
211
213
// Add file params
@@ -237,15 +239,15 @@ public HttpEntity getEntity() {
237
239
}
238
240
239
241
private void init (){
240
- urlParams = new ConcurrentHashMap < String , String >( );
242
+ urlParams = Collections . synchronizedList ( new ArrayList < BasicNameValuePair >() );
241
243
fileParams = new ConcurrentHashMap <String , FileWrapper >();
242
244
}
243
245
244
246
protected List <BasicNameValuePair > getParamsList () {
245
247
List <BasicNameValuePair > lparams = new LinkedList <BasicNameValuePair >();
246
248
247
- for (ConcurrentHashMap . Entry < String , String > entry : urlParams . entrySet () ) {
248
- lparams .add (new BasicNameValuePair ( entry . getKey (), entry . getValue ()) );
249
+ for (BasicNameValuePair pair : urlParams ) {
250
+ lparams .add (pair );
249
251
}
250
252
251
253
return lparams ;
0 commit comments