Monika Arora
Monika Arora

Reputation: 149

how to send Image file with Retrofit(@Fields)

Here I m using @Fields data with @FormUrlEncoded But I have to use both in same API @Part("user_image") RequestBody file with @Multipart. How does it possible? Thanks in advance.

@FormUrlEncoded
@POST("/datingapp/index.php/Webservice")
Call<Result> signupUser(@Field("user_name") String name,
                             @Field("age") String age,
                             @Field("work") String work,
                             @Field("home_town") String home_town,
                             @Field("gender") String gender,
                             @Field("interest") String interest,
                             @Field("study") String study,
                             @Field("email") String email,
                             @Field("password") String password,
                             @Field("device_id") String device_id,
                             @Field("device_type") String device_type,
                             @Part("user_image") RequestBody file,
                             @Field("signup") String signup); 

Upvotes: 7

Views: 5068

Answers (3)

Lionel Briand
Lionel Briand

Reputation: 1813

Http protocol not allow 2 Content-Type in the same request. So you have to choose :

  • application/x-www-form-urlencoded
  • multipart/form-data

You use application/x-www-form-urlencoded by using annotation @FormUrlEncoded in order to send image you have to transform the whole file into text (e.g. base64).

A better approach would be use multipart/form-data by describing your request like that :

@Multipart
@POST("/datingapp/index.php/Webservice")
Call<Result> signupUser(@Part("user_name") String name,
                         @Part("age") String age,
                         @Part("work") String work,
                         @Part("home_town") String home_town,
                         @Part("gender") String gender,
                         @Part("interest") String interest,
                         @Part("study") String study,
                         @Part("email") String email,
                         @Part("password") String password,
                         @Part("device_id") String device_id,
                         @Part("device_type") String device_type,
                         @Part("user_image") RequestBody file,
                         @Part("signup") String signup); 

Upvotes: 8

Kapil Rajput
Kapil Rajput

Reputation: 11545

Make api call like this :

@POST("/datingapp/index.php/Webservice")
@FormUrlEncoded
@Multipart
Call<Result> signupUser(@FieldMap LinkedHashMap<String, String> data,@Part RequestBody file);

and pass your data is the form of key and value in LinkedHashMap like this

LinkedHashMap<String, String> data = new LinkedHashMap<>();
data.put("user_name", user_name);
data.put("age", age);
data.put("work", work);
data.put("work", work);
data.put("gender", gender); and so on ....

for getting image in Multiparts :

RequestBody file= RequestBody.create(MediaType.parse("image/jpeg"), file);

final call to hit the api :

Call<Result> call = apiService.signupUser(data ,file);

Hope this works :)

Upvotes: -3

Nigam Patro
Nigam Patro

Reputation: 2770

@Multipart
@POST("/datingapp/index.php/Webservice")
Call<Result> signupUser(@PartMap Map<String,String> queryMap,
                             @Part("user_image") RequestBody file); 

Here the @PartMap contains the required other parameters which is nothing but a HashMap containing key and values e.g,

LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
map.put("user_name",username);

like above and so on.

Upvotes: 0

Related Questions