Skip to content

Commit ad4fc12

Browse files
Merge pull request android-async-http#1 from loopj/master
合并原作者的更新
2 parents e8615d0 + a69f563 commit ad4fc12

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed

library/src/main/java/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,10 @@ public void setBasicAuth(String username, String password) {
634634
*
635635
* @param username Basic Auth username
636636
* @param password Basic Auth password
637-
* @param preemtive sets authorization in preemtive manner
637+
* @param preemptive sets authorization in preemptive manner
638638
*/
639-
public void setBasicAuth(String username, String password, boolean preemtive) {
640-
setBasicAuth(username, password, null, preemtive);
639+
public void setBasicAuth(String username, String password, boolean preemptive) {
640+
setBasicAuth(username, password, null, preemptive);
641641
}
642642

643643
/**
@@ -659,12 +659,12 @@ public void setBasicAuth(String username, String password, AuthScope scope) {
659659
* @param username Basic Auth username
660660
* @param password Basic Auth password
661661
* @param scope an AuthScope object
662-
* @param preemtive sets authorization in preemtive manner
662+
* @param preemptive sets authorization in preemptive manner
663663
*/
664-
public void setBasicAuth(String username, String password, AuthScope scope, boolean preemtive) {
664+
public void setBasicAuth(String username, String password, AuthScope scope, boolean preemptive) {
665665
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
666666
setCredentials(scope, credentials);
667-
setAuthenticationPreemptive(preemtive);
667+
setAuthenticationPreemptive(preemptive);
668668
}
669669

670670
public void setCredentials(AuthScope authScope, Credentials credentials) {
@@ -676,16 +676,16 @@ public void setCredentials(AuthScope authScope, Credentials credentials) {
676676
}
677677

678678
/**
679-
* Sets HttpRequestInterceptor which handles authorization in preemtive way, as workaround you
679+
* Sets HttpRequestInterceptor which handles authorization in preemptive way, as workaround you
680680
* can use call `AsyncHttpClient.addHeader("Authorization","Basic base64OfUsernameAndPassword==")`
681681
*
682-
* @param isPreemtive whether the authorization is processed in preemtive way
682+
* @param isPreemptive whether the authorization is processed in preemptive way
683683
*/
684-
public void setAuthenticationPreemptive(boolean isPreemtive) {
685-
if (isPreemtive) {
686-
httpClient.addRequestInterceptor(new PreemtiveAuthorizationHttpRequestInterceptor(), 0);
684+
public void setAuthenticationPreemptive(boolean isPreemptive) {
685+
if (isPreemptive) {
686+
httpClient.addRequestInterceptor(new PreemptiveAuthorizationHttpRequestInterceptor(), 0);
687687
} else {
688-
httpClient.removeRequestInterceptorByClass(PreemtiveAuthorizationHttpRequestInterceptor.class);
688+
httpClient.removeRequestInterceptorByClass(PreemptiveAuthorizationHttpRequestInterceptor.class);
689689
}
690690
}
691691

library/src/main/java/com/loopj/android/http/PreemtiveAuthorizationHttpRequestInterceptor.java renamed to library/src/main/java/com/loopj/android/http/PreemptiveAuthorizationHttpRequestInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
import java.io.IOException;
3535

36-
public class PreemtiveAuthorizationHttpRequestInterceptor implements HttpRequestInterceptor {
36+
public class PreemptiveAuthorizationHttpRequestInterceptor implements HttpRequestInterceptor {
3737

3838
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
3939
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

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

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
* String[] colors = { "blue", "yellow" }; // Ordered collection
7474
* params.put("colors", colors); // url params: "colors[]=blue&colors[]=yellow"
7575
*
76+
* File[] files = { new File("pic.jpg"), new File("pic1.jpg") }; // Ordered collection
77+
* params.put("files", files); // url params: "files[]=pic.jpg&files[]=pic1.jpg"
78+
*
7679
* List<Map<String, String>> listOfMaps = new ArrayList<Map<String,
7780
* String>>();
7881
* Map<String, String> user1 = new HashMap<String, String>();
@@ -106,14 +109,15 @@ public class RequestParams implements Serializable {
106109
protected final ConcurrentHashMap<String, String> urlParams = new ConcurrentHashMap<String, String>();
107110
protected final ConcurrentHashMap<String, StreamWrapper> streamParams = new ConcurrentHashMap<String, StreamWrapper>();
108111
protected final ConcurrentHashMap<String, FileWrapper> fileParams = new ConcurrentHashMap<String, FileWrapper>();
112+
protected final ConcurrentHashMap<String, List<FileWrapper> >fileArrayParams = new ConcurrentHashMap<String, List<FileWrapper>>();
109113
protected final ConcurrentHashMap<String, Object> urlParamsWithObjects = new ConcurrentHashMap<String, Object>();
110114
protected String contentEncoding = HTTP.UTF_8;
111115

112116
/**
113117
* Sets content encoding for return value of {@link #getParamString()} and {@link
114118
* #createFormEntity()} <p>&nbsp;</p> Default encoding is "UTF-8"
115119
*
116-
* @param encoding String constant from {@link org.apache.http.protocol.HTTP}
120+
* @param encoding String constant from {@link HTTP}
117121
*/
118122
public void setContentEncoding(final String encoding) {
119123
if (encoding != null) {
@@ -200,12 +204,47 @@ public void put(String key, String value) {
200204
}
201205
}
202206

207+
/**
208+
* Adds files array to the request.
209+
*
210+
* @param key the key name for the new param.
211+
* @param files the files array to add.
212+
* @throws FileNotFoundException
213+
*/
214+
public void put(String key, File files[]) throws FileNotFoundException {
215+
put(key, files, null, null);
216+
}
217+
218+
/**
219+
*
220+
* Adds files array to the request with both custom provided file content-type and files name
221+
*
222+
* @param key the key name for the new param.
223+
* @param files the files array to add.
224+
* @param contentType the content type of the file, eg. application/json
225+
* @param customFileName file name to use instead of real file name
226+
* @throws FileNotFoundException throws if wrong File argument was passed
227+
*/
228+
public void put(String key, File files[], String contentType, String customFileName) throws FileNotFoundException {
229+
230+
if(key != null){
231+
List<FileWrapper> fileWrappers = new ArrayList<FileWrapper>();
232+
for (int i=0;i<files.length;i++){
233+
if(files[i] == null || !files[i].exists()){
234+
throw new FileNotFoundException();
235+
}
236+
fileWrappers.add(new FileWrapper(files[i], contentType, customFileName));
237+
}
238+
fileArrayParams.put(key,fileWrappers);
239+
}
240+
}
241+
203242
/**
204243
* Adds a file to the request.
205244
*
206245
* @param key the key name for the new param.
207246
* @param file the file to add.
208-
* @throws java.io.FileNotFoundException throws if wrong File argument was passed
247+
* @throws FileNotFoundException throws if wrong File argument was passed
209248
*/
210249
public void put(String key, File file) throws FileNotFoundException {
211250
put(key, file, null, null);
@@ -217,7 +256,7 @@ public void put(String key, File file) throws FileNotFoundException {
217256
* @param key the key name for the new param.
218257
* @param file the file to add.
219258
* @param customFileName file name to use instead of real file name
220-
* @throws java.io.FileNotFoundException throws if wrong File argument was passed
259+
* @throws FileNotFoundException throws if wrong File argument was passed
221260
*/
222261
public void put(String key, String customFileName, File file) throws FileNotFoundException {
223262
put(key, file, null, customFileName);
@@ -229,7 +268,7 @@ public void put(String key, String customFileName, File file) throws FileNotFoun
229268
* @param key the key name for the new param.
230269
* @param file the file to add.
231270
* @param contentType the content type of the file, eg. application/json
232-
* @throws java.io.FileNotFoundException throws if wrong File argument was passed
271+
* @throws FileNotFoundException throws if wrong File argument was passed
233272
*/
234273
public void put(String key, File file, String contentType) throws FileNotFoundException {
235274
put(key, file, contentType, null);
@@ -242,7 +281,7 @@ public void put(String key, File file, String contentType) throws FileNotFoundEx
242281
* @param file the file to add.
243282
* @param contentType the content type of the file, eg. application/json
244283
* @param customFileName file name to use instead of real file name
245-
* @throws java.io.FileNotFoundException throws if wrong File argument was passed
284+
* @throws FileNotFoundException throws if wrong File argument was passed
246285
*/
247286
public void put(String key, File file, String contentType, String customFileName) throws FileNotFoundException {
248287
if (file == null || !file.exists()) {
@@ -369,6 +408,7 @@ public void remove(String key) {
369408
streamParams.remove(key);
370409
fileParams.remove(key);
371410
urlParamsWithObjects.remove(key);
411+
fileArrayParams.remove(key);
372412
}
373413

374414
/**
@@ -381,7 +421,8 @@ public boolean has(String key) {
381421
return urlParams.get(key) != null ||
382422
streamParams.get(key) != null ||
383423
fileParams.get(key) != null ||
384-
urlParamsWithObjects.get(key) != null;
424+
urlParamsWithObjects.get(key) != null ||
425+
fileArrayParams.get(key) != null;
385426
}
386427

387428
@Override
@@ -414,6 +455,15 @@ public String toString() {
414455
result.append("FILE");
415456
}
416457

458+
for (ConcurrentHashMap.Entry<String, List<FileWrapper>> entry : fileArrayParams.entrySet()) {
459+
if (result.length() > 0)
460+
result.append("&");
461+
462+
result.append(entry.getKey());
463+
result.append("=");
464+
result.append("FILE");
465+
}
466+
417467
List<BasicNameValuePair> params = getParamsList(null, urlParamsWithObjects);
418468
for (BasicNameValuePair kv : params) {
419469
if (result.length() > 0)
@@ -469,7 +519,7 @@ public void setAutoCloseInputStreams(boolean flag) {
469519
public HttpEntity getEntity(ResponseHandlerInterface progressHandler) throws IOException {
470520
if (useJsonStreamer) {
471521
return createJsonStreamerEntity(progressHandler);
472-
} else if (!forceMultipartEntity && streamParams.isEmpty() && fileParams.isEmpty()) {
522+
} else if (!forceMultipartEntity && streamParams.isEmpty() && fileParams.isEmpty() && fileArrayParams.isEmpty()) {
473523
return createFormEntity();
474524
} else {
475525
return createMultipartEntity(progressHandler);
@@ -553,6 +603,14 @@ private HttpEntity createMultipartEntity(ResponseHandlerInterface progressHandle
553603
entity.addPart(entry.getKey(), fileWrapper.file, fileWrapper.contentType, fileWrapper.customFileName);
554604
}
555605

606+
// Add file collection
607+
for (ConcurrentHashMap.Entry<String, List<FileWrapper>> entry : fileArrayParams.entrySet()) {
608+
List<FileWrapper> fileWrapper = entry.getValue();
609+
for (FileWrapper fw:fileWrapper){
610+
entity.addPart(entry.getKey(), fw.file, fw.contentType, fw.customFileName);
611+
}
612+
}
613+
556614
return entity;
557615
}
558616

0 commit comments

Comments
 (0)