7

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); 
Monika Arora
  • 149
  • 1
  • 10
  • You can send an image in the form of Base64 string. I never used Retrofit although have a look [HERE](http://stackoverflow.com/questions/22787585/android-retrofit-base64-body) and [HERE](http://stackoverflow.com/questions/32009612/retrofit-send-base64-encoded-string-in-parameters) – Devendra Singh Dec 02 '16 at 07:20

3 Answers3

8

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); 
Lionel Briand
  • 1,732
  • 2
  • 13
  • 21
0
@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.

Nigam Patro
  • 2,760
  • 1
  • 18
  • 33
-3

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 :)

Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
  • 6
    `@Multipart` annotation can not use with `@FormUrlEncoded`, and `@FieldMap` shoule be `@PartMap` – Ninja Jan 18 '17 at 04:21