Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

I'm developing an application where at a point the user has to choose any number of countries from a list and I must send the selected names through a multipart.

I am not uploading any file along with the String[], but there is no route to upload information without it being a multipart and I don't have any saying in how the web server operates.

I've attempted to simply send it as an Array, ArrayList and JsonArray as such:

@Headers({
    "Connection: Keep-Alive",
})
@Multipart
@PUT("/user/{id}")
String updateUser(@Path("id") int userId, @Part("user[countries]") String[] countries);

I've also attempted this solution, but I either misunderstood it or it does not work. Here is the code I attempted to use:

ArrayList<String> countries = CountryManager.getInstance().getSelectedCountryIds();
RequestBody requestBody;
LinkedHashMap<String, RequestBody> hashMap = new LinkedHashMap<>();

for(int i = 0; i < countries.size(); i++) {
    requestBody = RequestBody.create(MediaType.parse("text/plain"), countries.get(i));
    hashMap.put("countries["+i+"]", requestBody);
}

And changing the retrofit method accordingly:

@Headers({
    "Connection: Keep-Alive",
})
@Multipart
@PUT("/user/{id}")
String updateUser(@Path("id") int userId, @PartMap() Map countries);

However in all attempts I've gotten the error retrofit.RetrofitError: Part body must not be null.

I've also mentioned that the ChangeLog for retrofit mentions "New: Support iterable and array @Part parameters using OkHttp's MultipartBody.Part", but after some digging, I've found the given example quite confusing to the point that I'm unsure on how to implement it in my code and was unable to find a tutorial that even mentioned sending arrays in a multipart.

Is such a thing simply impossible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...