相关疑难解决方法(0)

Retrofit 2无法上传带有两个额外字符串参数的文件

在问题的底部阅读编辑以寻找可能的替代解决方案,直到找到解决方案.

这是一个成功的帖子文件,其中包含两个使用POSTMan的参数.我试图通过改造来做同样的事但收到BadRequest.

邮差设置:

在此输入图像描述

Chrome网络帖子详情: 在此输入图像描述

现在我在Android中如何做到这一点但失败了:

改造服务接口:

@Multipart
@POST("jobDocuments/upload")
Call<ResponseBody> upload(@Part("file") MultipartBody.Part file,@Part("folder") MultipartBody.Part folder,@Part("name") MultipartBody.Part name);
Run Code Online (Sandbox Code Playgroud)

这是我的@Background方法,用于生成上述服务来运行网络请求

CustDataClient service =
            ServiceGenerator.createService(CustDataClient.class);
    File file = new File(fileUri.getPath());
    // create RequestBody instance from file
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("multipart/form-data"), file);

    MultipartBody.Part fileData =
            MultipartBody.Part.createFormData("file", fileName, requestFile);
    MultipartBody.Part folder =
            MultipartBody.Part.createFormData("folder", "LeadDocuments");
    MultipartBody.Part name =
            MultipartBody.Part.createFormData("name", fileName);
    // finally, execute the request
    Call<ResponseBody> call = service.upload(fileData,folder,name);
    try {
        Response<ResponseBody> rr = call.execute();
        ResponseBody empJobDocsResult = rr.body();//Bad Request here :(
        Log.v("Upload", "success");
    } catch …
Run Code Online (Sandbox Code Playgroud)

android file-upload android-networking retrofit retrofit2

15
推荐指数
3
解决办法
2万
查看次数