使用PartMap Retrofit 2上传多个文件

时间:2016-09-14 09:57:51

标签: android retrofit retrofit2

发布新产品时,我的服务器需要使用密钥来捕获文件。对于有多少文件没有限制。文件无限制。

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

如果我编辑服务器,它就不再向后兼容了。我需要让Android应用程序像Retrofit 1.9一样工作。

以下是我的实施方式。

改造1.9

创建ApiService接口的类。

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。

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接口的类。

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

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

1 个答案:

答案 0 :(得分:1)

我已应用this solution,现在已修复。

以下是我的实施方式。

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);
相关问题