Is it possible to send a String[] through Multipart using Retrofit?

故事扮演 提交于 2019-12-01 05:25:15

I know I am late for this answer. you can use @Query("someKey[]") for sending arraylist in multipart. Heres is the working example.

@Multipart
    @POST("./")
    Call<JsonElement> addSubEvent(@Part(EndAPI.USE_CASE) RequestBody useCase,
                                  @Query("event_id[]") ArrayList<String> event_id,
                                  @Query("user_id[]") ArrayList<String> user_id,
                                  @Query("name[]") ArrayList<String> name,
                                  @Query("date_time[]") ArrayList<String> date_time,
                                  @Part("token") RequestBody token,
                                  @Part MultipartBody.Part... profilePic);

Hope It will help some one seeking for the answer.

creating multi part list to use as a array list

List<MultipartBody.Part> descriptionList = new ArrayList<>();
descriptionList.add(MultipartBody.Part.createFormData("param_name_here", values));

following is the function in service interface of retrofit.

@PUT("/")
@Multipart
Call<ResponseBody> uploadPhotos(
        @Part MultipartBody.Part placeId,
        @Part MultipartBody.Part name,
        @Part List<MultipartBody.Part> desclist, // <-- use such for list of same parameter
        @Part List<MultipartBody.Part> files  // <-- multiple photos here
);

Hope it helps someone. cheers... !!!

Just use @Part("items[]") List items

Like this:

List<String> items = new ArrayList();
    items.add("1");
    items.add("2");
    items.add("3");

@Multipart
@POST("/")
Call<Result> yourMethod(@PartMap() Map<String, RequestBody> partMap, @PartMap() Map<String, RequestBody> map, @Part MultipartBody.Part file, @Part("items[]") List<String> items);

this is possible and that too using multiparts, just convert your Array into Json String and add it as a single part and once this Json is received at Server side, just deserialize it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!