1

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?

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
Dr.Kong
  • 25
  • 8

2 Answers2

2

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.

  • Thanks dear friend for your help. i want just upload image and not send any data. **(just image and image name)** . for this can i use this code : `@Part MultipartBody.Part image)` ? – Dr.Kong May 29 '18 at 05:57
2

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.

Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52