Before I post this question here, I have tried to add @Multipart
above interface method And searching in stackoverflow still cannot find similar with my problem.
In this case, I try to send image using TypedFile
to server. My interface method look like this :
@Headers({"Content-type: application/json"})
@POST("/user/change")
void postChange(@Query("name") String name, @Query("email") String email, @Query("password") String password, @Query("phone") String phone, @Query("user_id") String userId, @Query("address[]") String[] listAddress, @Query("head[]") String[] head, @Part("photo_profile") TypedFile photoProfile, @Body TypedInput jsonObject, Callback<ReceiveDTO> callback);
EDIT
In that method we can see @Part
and @Body
. If i add @Multipart
above the method, it will we throw an error @Body parameters cannot be used with form or multi-part encoding. (parameter #9)
I am using Retrofit 1.9
For anyone with the same issue, make sure to add the @Multipart
annotation above your @POST/@PUT
. I had the same error and my problem was just that I was missing that @Multipart
annotation.
We use @Query only with Get
Request and in fact @Query append parameters at the end of URL, See Document examples.
If you need to send user profile to server, Use MultiPart
:
Multipart parts use one of Retrofit's converters or they can implement RequestBody to handle their own serialization.
For example in following piece of code we can send Profile photo with some description to server:
@Multipart
@PUT("user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);
You can even add more additional attributes with @Part. See complete example here which step by step explained how to do this.
Edit: As JackWarthon explain here, The @Body annotation defines a single request body.
interface Foo {
@POST("/jayson")
FooResponse postJson(@Body FooRequest body);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With