18
18
19
19
package com .loopj .android .http ;
20
20
21
+ import java .io .ByteArrayInputStream ;
22
+ import java .io .File ;
21
23
import java .io .UnsupportedEncodingException ;
22
24
import java .util .LinkedList ;
23
25
import java .util .List ;
32
34
public class RequestParams {
33
35
private static String ENCODING = "UTF-8" ;
34
36
35
- protected ConcurrentHashMap <String ,String > urlParams ;
37
+ protected ConcurrentHashMap <String , String > stringParams ;
38
+ protected ConcurrentHashMap <String , FileWrapper > fileParams ;
36
39
37
40
public RequestParams () {
38
41
init ();
@@ -54,39 +57,125 @@ public RequestParams(String key, String value) {
54
57
55
58
public void put (String key , String value ){
56
59
if (key != null && value != null ) {
57
- urlParams .put (key ,value );
60
+ stringParams .put (key , value );
61
+ }
62
+ }
63
+
64
+ public void put (String key , ByteArrayInputStream filedata ) {
65
+ put (key , filedata , null , null );
66
+ }
67
+
68
+ public void put (String key , ByteArrayInputStream filedata , String filename ) {
69
+ put (key , filedata , filename , null );
70
+ }
71
+
72
+ public void put (String key , ByteArrayInputStream filedata , String filename , String contentType ){
73
+ if (key != null && filedata != null ) {
74
+ fileParams .put (key , new FileWrapper (filedata , filename , contentType ));
58
75
}
59
76
}
60
77
61
78
public void remove (String key ){
62
- urlParams .remove (key );
79
+ stringParams .remove (key );
80
+ fileParams .remove (key );
63
81
}
64
82
65
83
public String getParamString () {
84
+ if (!fileParams .isEmpty ()) {
85
+ throw new RuntimeException ("Uploading files is not supported with Http GET requests." );
86
+ }
87
+
66
88
return URLEncodedUtils .format (getParamsList (), ENCODING );
67
89
}
68
90
69
91
public HttpEntity getEntity () {
70
92
HttpEntity entity = null ;
71
- try {
72
- entity = new UrlEncodedFormEntity (getParamsList (), ENCODING );
73
- } catch (UnsupportedEncodingException e ) {
74
- e .printStackTrace ();
93
+
94
+ if (!fileParams .isEmpty ()) {
95
+ SimpleMultipartEntity multipartEntity = new SimpleMultipartEntity ();
96
+
97
+ // Add string params
98
+ for (ConcurrentHashMap .Entry <String , String > entry : stringParams .entrySet ()) {
99
+ multipartEntity .addPart (entry .getKey (), entry .getValue ());
100
+ }
101
+
102
+ // Add file params
103
+ for (ConcurrentHashMap .Entry <String , FileWrapper > entry : fileParams .entrySet ()) {
104
+ FileWrapper file = entry .getValue ();
105
+ if (file .bytes != null ) {
106
+ String filename = file .filename ;
107
+ if (filename == null ) {
108
+ filename = "nofilename" ;
109
+ }
110
+
111
+ if (file .contentType != null ) {
112
+ multipartEntity .addPart (entry .getKey (), filename , file .bytes , file .contentType );
113
+ } else {
114
+ multipartEntity .addPart (entry .getKey (), filename , file .bytes );
115
+ }
116
+ }
117
+ }
118
+
119
+ entity = multipartEntity ;
120
+ } else {
121
+ try {
122
+ entity = new UrlEncodedFormEntity (getParamsList (), ENCODING );
123
+ } catch (UnsupportedEncodingException e ) {
124
+ e .printStackTrace ();
125
+ }
75
126
}
127
+
76
128
return entity ;
77
129
}
78
130
79
131
private void init (){
80
- urlParams = new ConcurrentHashMap <String ,String >();
132
+ stringParams = new ConcurrentHashMap <String , String >();
133
+ fileParams = new ConcurrentHashMap <String , FileWrapper >();
81
134
}
82
135
83
136
private List <BasicNameValuePair > getParamsList () {
84
137
List <BasicNameValuePair > lparams = new LinkedList <BasicNameValuePair >();
85
138
86
- for (ConcurrentHashMap .Entry <String , String > entry : urlParams .entrySet ()) {
139
+ for (ConcurrentHashMap .Entry <String , String > entry : stringParams .entrySet ()) {
87
140
lparams .add (new BasicNameValuePair (entry .getKey (), entry .getValue ()));
88
141
}
89
142
90
143
return lparams ;
91
144
}
145
+
146
+ @ Override
147
+ public String toString () {
148
+ StringBuilder result = new StringBuilder ();
149
+ for (ConcurrentHashMap .Entry <String , String > entry : stringParams .entrySet ()) {
150
+ if (result .length () > 0 )
151
+ result .append ("&" );
152
+
153
+ result .append (entry .getKey ());
154
+ result .append ("=" );
155
+ result .append (entry .getValue ());
156
+ }
157
+
158
+ for (ConcurrentHashMap .Entry <String , FileWrapper > entry : fileParams .entrySet ()) {
159
+ if (result .length () > 0 )
160
+ result .append ("&" );
161
+
162
+ result .append (entry .getKey ());
163
+ result .append ("=" );
164
+ result .append ("FILE" );
165
+ }
166
+
167
+ return result .toString ();
168
+ }
169
+
170
+ private static class FileWrapper {
171
+ public ByteArrayInputStream bytes ;
172
+ public String filename ;
173
+ public String contentType ;
174
+
175
+ public FileWrapper (ByteArrayInputStream bytes , String filename , String contentType ) {
176
+ this .bytes = bytes ;
177
+ this .filename = filename ;
178
+ this .contentType = contentType ;
179
+ }
180
+ }
92
181
}
0 commit comments