Android okHttp addFormDataPart 动态多张图片 [英] Android okHttp addFormDataPart dynamically for Multiple Image

查看:44
本文介绍了Android okHttp addFormDataPart 动态多张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好 AndroidUploader,

Hello AndroidUploaders,

我已经给出了答案使用 OkHttp 多部分上传大文件,但我被卡住了多张图片上传.

I had given answer Uploading a large file in multipart using OkHttp but i am stuck with multiple image uploading.

我想一次动态上传1 到 10 张图片.

I want to upload dynamically 1 to 10 image at a time.

RequestBody requestBody = new MultipartBuilder()
                    .type(MultipartBuilder.FORM)
                    .addFormDataPart(KEY_PHOTO_CAPTION, photoCaption)
                    .addFormDataPart(KEY_FILE, "profile.png", RequestBody.create(MEDIA_TYPE_PNG, sourceFile))
                    .build();

我有 PhotoCaption 类的 ArrayList,其中有 captionPhotourlPhoto 那么我如何addFormDataPart()

I have ArrayList of PhotoCaption class which has captionPhoto and urlPhoto so how can i addFormDataPart()

我正在考虑进行循环并多次调用 ArrayList 大小的函数.

I am thinking to make loop and call this function that many times of ArrayList size.

动态使用addFormDataPart()有什么解决办法吗?

Is there any solution to addFormDataPart() use dynamically?

推荐答案

此答案适用于 OkHttp2

对于 OkHttp3,您可以查看这篇文章.

For OkHttp3 You can see this post.

对于多个图像,您只需要根据您的要求运行循环,与请求相关的其余部分将与您相同.

For multiple image you just need to run the loop as per your requirement, remaining part related to request will be same as you do.

  //  final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
final MediaType MEDIA_TYPE=MediaType.parse(AppConstant.arrImages.get(i).getMediaType());

//If you can have multiple file types, set it in ArrayList
                    MultipartBuilder buildernew = new MultipartBuilder().type(MultipartBuilder.FORM)
                            .addFormDataPart("title", title)

                    for (int i = 0; i < AppConstants.arrImages.size(); i++) {
                        File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                                TEMP_FILE_NAME + i + ".png");
                        if (f.exists()) {
                            buildernew.addFormDataPart(TEMP_FILE_NAME + i, TEMP_FILE_NAME + i + FILE_EXTENSION, RequestBody.create(MEDIA_TYPE, f));
                        }
                    }
                    RequestBody requestBody = buildernew.build();
                    Request request = new Request.Builder()
                            .url(Url.URL + Url.INSERT_NEWS)
                            .post(requestBody)
                            .build();

                    OkHttpClient client = new OkHttpClient();
                    Response response = client.newCall(request).execute();
                    return response.body().string();

不要忘记删除临时文件.您上传的文件如果没有用.

Dont forget to delete temp. files that you uploaded if it is of no use.

这篇关于Android okHttp addFormDataPart 动态多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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