如何使用AsyncHttpClient Android上传多个文件 [英] How to upload multiple files with AsyncHttpClient Android

查看:185
本文介绍了如何使用AsyncHttpClient Android上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以从AsyncHttpClient上传单个文件

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?

推荐答案

您可以传递文件数组作为files键的值. 为此,请遵循以下代码:

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) {

}

另外,如果需要动态数组,可以使用ArrayList 并使用.toArray()

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);

希望获得帮助. = D

Hope this help. =D

这篇关于如何使用AsyncHttpClient Android上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆