How to send image file using Retrofit (@Fields)

Here I am using @Fieldsdata with @FormUrlEncodedBut I need to use both APIs @Part("user_image") RequestBody filewith @Multipart. How is this 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); 
+4
source share
3 answers

Http does not allow 2 Content-Type in the same request. Therefore you need to choose:

  • application / x-www-form-urlencoded
  • multi-part / form data

You use application/x-www-form-urlencodedusing annotations @FormUrlEncodedto send an image, you need to convert the entire file to text (e.g. base64).

A better approach would be to use multipart/form-data, describing your request as follows:

@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); 
+4
@Multipart
@POST("/datingapp/index.php/Webservice")
Call<Result> signupUser(@PartMap Map<String,String> queryMap,
                             @Part("user_image") RequestBody file); 

@PartMap , , HashMap , ,

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

..

0

api- :

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

- LinkedHashMap,

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 ....

Multiparts:

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

, api:

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

, :)

-1

Source: https://habr.com/ru/post/1662600/


All Articles