Retrofit @body with @multipart having Issue

后端 未结 8 1573
星月不相逢
星月不相逢 2020-12-25 08:54

Image Multipart in class type object.

case 1. (Which I had done)

Service params:

{\"id\":\"1\",\"name\":\"vishal\",\"image/file\":\"\"} 
         


        
相关标签:
8条回答
  • 2020-12-25 09:18

    this works for me.

    What I did was add every additional params using:

    MultipartBody.Part Partname = MultipartBody.Part.createFormData("ParamName", "Value");
    

    Mabe you don't need to create another body, but just add others params apart from the file or whatever you are sending. finally at the interface I put as a params every bodypart that I need.

    @Multipart
    @POST("api/service/uploadVideo")
    Call<ResponseBody> uploadVideoToServer(
            @Part MultipartBody.Part video,
            @Part MultipartBody.Part param2,
            @Part MultipartBody.Part param3 ....
    );
    
    0 讨论(0)
  • 2020-12-25 09:20

    You can also use a Map with RequestBody as value and string as keys to add parameters and you can send this using Multipart and PartMap.

    Check the following code, hope it will help :

    @Multipart
    @POST("/add")
    Call<ResponseBody> addDocument(@PartMap Map<String,RequestBody> params);
    
    Map<String, RequestBody> map = new HashMap<>();
    
    map.put("user_id", RequestBody.create(MediaType.parse("multipart/form-data"), SessionManager.getInstance().getCurrentUserId()));
    map.put("doc_name", RequestBody.create(MediaType.parse("multipart/form-data"), CommonUtils.removeExtension(textFile.getName())));
    map.put("doc_category", RequestBody.create(MediaType.parse("multipart/form-data"), category));
    map.put("doc_image_file", RequestBody.create(MediaType.parse("multipart/form-data"), imageFile));
    map.put("doc_text_content", RequestBody.create(MediaType.parse("multipart/form-data"), body));
    map.put("doc_update_time", RequestBody.create(MediaType.parse("multipart/form-data"), "" + new Date(textFile.lastModified())));
    
    0 讨论(0)
提交回复
热议问题