简体   繁体   中英

Difference between Part and PartMap in Retrofit2 in Android

In my application i want upload image from phone to server with Retrofit2 .
For this job i find some sources from internet, but in one source use this :

public interface RetrofitInterface {
    @Multipart
    @POST("/images/upload")
    Call<Response> uploadImage(@Part MultipartBody.Part image);
}

and in other source this below :

public interface ApiConfig {

    @Multipart
    @POST("images/upload_image.php")
    Call<ServerResponse> upload(
            @PartMap Map<String, RequestBody> map);
}

In first source use @Part MultipartBody.Part image and in second source use @PartMap Map<String, RequestBody> map .

What's the difference between the two?

Which one do I use better?

What's the difference between the two?

@Part is used during this scenario ,
When you have a multi part request and you know before hand the no of files that needs to be sent to the server you declare it with @part annotation.

@PartMap is used during this scenario ,
When you don't know the no of parts that has to be sent to the server under the same key we use @PartMap annoation

Now to answer your question Which one do I use better?

If you have a limites set of images that you have to upload go with @Part approach else use @PartMap.

You can check retrofit 2 document for understanding differences between part & PartMap :

Differences between Part & PartMap for uploading files

Retrofit Documentation: If you just need to pass a single or two descriptions with a file, you can just declare it as a @Part in your service . This works great for small use cases, but if you need to send more than a handful of properties, it gets quite messy, especially if not all of them are always set.

Retrofit offers an easy solution, which makes the uploads quite customizable: @PartMap. @PartMap is an additional annotation for a request parameter, which allows us to specify how many and which parts we send during runtime. This can very helpful if your form is very long, but only a few of those input field values are actually send.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM