【问题标题】:Upload multiple files with PartMap Retrofit 2使用 PartMap Retrofit 2 上传多个文件
【发布时间】:2016-09-14 19:49:55
【问题描述】:

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

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

【问题讨论】:

  • 你能把你打出来的文件贴出来吗
  • 文件是png、jpg等图片文件,没什么特别的。
  • 我也面临同样的问题...我想在single key 中发布多张图片

标签: android retrofit retrofit2


【解决方案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);

【讨论】:

    猜你喜欢
    • 2016-09-04
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2016-08-31
    • 2017-05-14
    • 2017-03-28
    • 2016-11-09
    相关资源
    最近更新 更多