Java Retrofti 2:发布多个具有部件名称的文件以及@FormUrlEncoded formData

Java Retrofti 2:发布多个具有部件名称的文件以及@FormUrlEncoded formData,java,android,retrofit,retrofit2,okhttp3,Java,Android,Retrofit,Retrofit2,Okhttp3,我有一个相当复杂的帖子,我想用它来写 @Multipart @FormUrlEncoded @POST("post") Call<JSONObject> createPost( @Field("name") String name, @Part("media_1") MultipartBody.Part mediaOne, @Part("media_2") MultipartBody.Part media

我有一个相当复杂的帖子,我想用它来写

    @Multipart
    @FormUrlEncoded
    @POST("post")
    Call<JSONObject> createPost(
        @Field("name") String name,
        @Part("media_1") MultipartBody.Part mediaOne,
        @Part("media_2") MultipartBody.Part mediaTwo,
        @Part("media_3") MultipartBody.Part mediaThree,
        @Part("media_4") MultipartBody.Part mediaFour,
        @Part("media_5") MultipartBody.Part mediaFive,
        @Header("secret_key") int secretKey
    );
@Multipart
@FormUrlEncoded
@职位(“职位”)
呼叫createPost(
@字段(“名称”)字符串名称,
@部分(“媒体1”)多部分主体。部分媒体1,
@第二部分(“媒体2”)多部分正文第二部分,
@第三部分(“媒体3”)多部分正文第三部分,
@第4部分(“媒体”)多部分正文第4部分,
@第5部分(“媒体”)多部分正文第5部分,
@标题(“密钥”)int secretKey
);
它包含多个方面,比如一个名为“name”的参数,我通常认为它是用@Field和@FormUrlEncoded发布的。我知道我不能同时对@Multipart和@FormUrlEncoded进行编码,所以我想我应该从上面的代码示例中删除@FormUrlEncoded,并用@Part替换@Field。这是正确的吗

然后,我得到异常“@Part parameters using the MultipartBody.Part不得在注释中包含零件名称”。但这并不是说我可以删除@Part(“media_1”)等,因为这些部分的名称可以确保媒体文件被上传到正确的位置。这里的解决方案是什么

这是我打过的最复杂的电话。感谢您抽出时间回顾我的问题

下面是我使用Reformation2调用的地方,以防它作为上下文有用:

 file0 = FileUtils.getFile(filePath);
 requestFile0 = RequestBody.create(MediaType.parse("multipart/form-data"), file0);
 body0 = MultipartBody.Part.createFormData("image0", file0.getName(), requestFile0);

 file1 = FileUtils.getFile(constructedLog.getLogImageLocations().get(1));
 requestFile1 = RequestBody.create(MediaType.parse("multipart/form-data"), file1);
 body1 = MultipartBody.Part.createFormData("logImage1", file1.getName(), requestFile1);

 //etc for file2, file3, file4

 Call<JSONObject> call = apiService.getApi().createPost(
            getName(),
            body0,
            body1,
            body2,
            body3,
            body4,
            secretKey
            );
file0=FileUtils.getFile(filePath);
requestFile0=RequestBody.create(MediaType.parse(“多部分/表单数据”),file0);
body0=MultipartBody.Part.createFormData(“image0”,file0.getName(),requestFile0);
file1=FileUtils.getFile(constructedLog.getLogImageLocations().get(1));
requestFile1=RequestBody.create(MediaType.parse(“多部分/表单数据”),file1);
body1=MultipartBody.Part.createFormData(“logImage1”,file1.getName(),requestFile1);
//文件2、文件3、文件4的etc
Call Call=apiService.getApi().createPost(
getName(),
body0,
主体1,
车身2,
车身3,
车身4,
秘钥
);

API调用的代码:

@Multipart
@POST("post")
Call<JSONObject> createPost(
    @Part("name") RequestBody name,
    @Part MultipartBody.Part mediaOne,
    @Part MultipartBody.Part mediaTwo,
    @Part MultipartBody.Part mediaThree,
    @Part MultipartBody.Part mediaFour,
    @Part MultipartBody.Part mediaFive,
    @Header("secret_key") int secretKey
);

File file = new File(yourpathhere);
body0 = MultipartBody.Part.createFormData("media_1", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));

RequestBody name = RequestBody.create(MediaType.parse("text/plain"), yourString);


Call<JSONObject> call = apiService.getApi().createPost(
        name,
        body0,
        body1,
        body2,
        body3,
        body4,
        secretKey
        );
@Multipart
@职位(“职位”)
呼叫createPost(
@部分(“名称”)请求主体名称,
@一部分是多部分主体,一部分是媒体主体,
@第二部分,第二部分,
@第三部分,第三部分,
@第四部分,第四部分,
@第五部分,第五部分,
@标题(“密钥”)int secretKey
);
File File=新文件(yourpath here);
body0=MultipartBody.Part.createFormData(“media_1”,file.getName(),RequestBody.create(MediaType.parse(“image/*”),file));
RequestBody name=RequestBody.create(MediaType.parse(“text/plain”),yourString);
Call Call=apiService.getApi().createPost(
名称
body0,
主体1,
车身2,
车身3,
车身4,
秘钥
);
本质上,问题是我在createFormData()和@Part中都声明了名称。你只能做其中一个

我还将第一个参数更改为RequestBody。如果你只是尝试使用一个字符串,至少在我的例子中,API会认为它周围应该有一个双引号

链接到该解决方案: