Retrofit @body with @multipart having Issue

后端 未结 8 1648
星月不相逢
星月不相逢 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: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 addDocument(@PartMap Map params);
    
    Map 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())));
    

提交回复
热议问题