使用 PartMap Retrofit 2 上传多个文件 [英] Upload multiple files with PartMap Retrofit 2

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

问题描述

发布新产品时,我的服务器需要使用密钥捕获文件.文件数量没有限制.文件不受限制.

When posting a new product, my server needs to catch files with keys. There's no limit to how many files. Files are unlimited.

使用 Retrofit 1.9,一切都很完美.更新到 Retrofit 2 后,我的服务器没有收到任何文件.

With Retrofit 1.9, everything worked perfect. After I update to Retrofit 2, my server does not receive any file.

如果我编辑服务器,它将不再向后兼容.我需要让 Android 应用像在 Retrofit 1.9 上一样工作.

If I edit the server, it won't be backward compatible anymore. I need to make the android app works like it worked with Retrofit 1.9.

这是我的实施方式.

用于创建 ApiService 接口的类.

class to create ApiService interface.

public class ApiClient {

    public interface ApiInterface {

        @Multipart
        @POST("/products/")
        void uploadProduct(
                @PartMap Map<String, String> params,
                @PartMap Map<String, TypedFile> files,
                Callback<Product> cb);

    }

}

使用 ApiService.

using the ApiService.

Map<String, String> params = new HashMap<>();
params.put("title", title);
params.put("price", price);
params.put("content", content);

Map<String, TypedFile> files = new HashMap<>();
for (int pos = 0; pos < photoPaths.length; pos++) {
    if (!Strings.isNullOrEmpty(photoPaths[pos])) {
        TypedFile typedFile = new TypedFile("multipart/form-data", new File(photoPaths[pos]));
        files.put("photo_" + String.valueOf(pos + 1), typedFile);
    }
}

apiInterface.uploadProduct(params, files, cb);

改造 2

用于创建 ApiService 接口的类.

Retrofit 2

class to create ApiService interface.

public class ApiClient {

    public interface ApiInterface {

        @Multipart
        @POST("/products/")
        Call<Product> uploadProduct(
                @PartMap Map<String, RequestBody> params,
                @PartMap Map<String, RequestBody> files);


    }

    public static final String MULTIPART_FORM_DATA = "multipart/form-data";

    public static RequestBody createRequestBody(@NonNull File file) {
        return RequestBody.create(
                MediaType.parse(MULTIPART_FORM_DATA), file);
    }

    public static RequestBody createRequestBody(@NonNull String s) {
        return RequestBody.create(
                MediaType.parse(MULTIPART_FORM_DATA), s);
    }

}

使用 ApiService

using the ApiService

Map<String, RequestBody> params = new HashMap<>();
params.put("title", ApiClient.createRequestBody(title));
params.put("price", ApiClient.createRequestBody(price));
params.put("content", ApiClient.createRequestBody(content));

Map<String, RequestBody> files = new HashMap<>();
for (int pos = 0; pos < photoPaths.length; pos++) {
    if (!TextUtils.isEmpty(photoPaths[pos])) {
        RequestBody requestBody = ApiClient.createRequestBody(new File(photoPaths[pos]));
        files.put("photo_" + String.valueOf(pos + 1), requestBody);
    }
}

Call<Product> call = apiInterface.uploadProduct(params, files);

推荐答案

我应用了这个解决方案,现在已经修复.

I applied this solution and it's now fixed.

这是我的实施方式.

Map<String, RequestBody> params = new HashMap<>();
params.put("title", ApiClient.createRequestBody(title));
params.put("price", ApiClient.createRequestBody(price));
params.put("content", ApiClient.createRequestBody(content));

Map<String, RequestBody> files = new HashMap<>();
for (int pos = 0; pos < photoPaths.length; pos++) {
    if (!TextUtils.isEmpty(photoPaths[pos])) {
        RequestBody requestBody = ApiClient.createRequestBody(new File(photoPaths[pos]));
        // fix is right here
        String key = String.format("%1$s\"; filename=\"%1$s", "photo_" + String.valueOf(pos + 1));
        files.put(key, requestBody);
    }
}

Call<Product> call = apiInterface.uploadProduct(params, files);

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

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