How to upload multiple files using AsyncHttpClient Android

I know that I can download one 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 a server with multiple messages. How can i do this?

+6
source share
5 answers

You can pass an array of files as a value for the file key. To do this, run the following code:

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

}

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

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

//...add File objects to fileArrayList

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

I hope for this help. = D

+3
source

Create a SimpleMultipartEntity object and call addPart for each file you want to download.

+1
 File[] files = lst.toArray(new File[lst.size()]);

    try {
        params.put("media[]", files);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
+1
source

You must pass all your files as a parameter in the parameters. For instance:

params.put("file_one", myFiles1);
params.put("file_two", myFiles2)
0
source

You should use this code:

public static void fileUpLoad(String url,File file,AsyncHttpResponseHandler asyncHttpResponseHandler){
    RequestParams requestParams=new RequestParams();

    try{
        requestParams.put("profile_picture", file,"application/octet-stream");
        client.post(url, requestParams, new AsyncHttpResponseHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
-1
source

All Articles