Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload multiple files with AsyncHttpClient Android

I know I can upload single file from AsyncHttpClient

http://loopj.com/android-async-http/

File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
    params.put("profile_picture", myFile);
} catch(FileNotFoundException e) {}

But I have to upload multiple files to the server with multipart post. How can I do that?

like image 789
Yogesh Maheshwari Avatar asked Aug 14 '12 08:08

Yogesh Maheshwari


2 Answers

You can pass a file array as value for the files key. In order to do that, follow the code below:

File[] myFiles = { new File("pic.jpg"), new File("pic1.jpg") }; 
RequestParams params = new RequestParams();
try {
    params.put("profile_picture[]", myFiles);
} catch(FileNotFoundException e) {

}

Aditionally, if you want a dynamic array, you can use an ArrayList and convert to File[] type with the method .toArray()

ArrayList<File> fileArrayList = new ArrayList<>();

//...add File objects to fileArrayList

File[] files = new File[fileArrayList.size()];  
fileArrayList.toArray(files);

Hope this help. =D

like image 161
Maurício Fonseca Avatar answered Sep 29 '22 19:09

Maurício Fonseca


Create the SimpleMultipartEntity object and call the addPart for each file that you want to upload.

like image 44
Rafael Sanches Avatar answered Sep 29 '22 20:09

Rafael Sanches