如何使用 Retrofit(@Fields) 发送图像文件
how to send Image file with Retrofit(@Fields)
我在这里使用 @Fields
数据和 @FormUrlEncoded
但我必须在相同的 API @Part("user_image") RequestBody file
和 @Multipart
中使用两者。怎么可能?提前致谢。
@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);
@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);
如上等等。
HTTP 协议不允许在同一个请求中使用 2 个 Content-Type。所以你必须选择:
- application/x-www-form-urlencoded
- multipart/form-data
您通过使用注释 @FormUrlEncoded
使用 application/x-www-form-urlencoded
为了发送图像,您必须将整个文件转换为文本(例如 base64)。
更好的方法是使用 multipart/form-data
来描述您的请求:
@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);
像这样调用 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);
希望这有效:)
我在这里使用 @Fields
数据和 @FormUrlEncoded
但我必须在相同的 API @Part("user_image") RequestBody file
和 @Multipart
中使用两者。怎么可能?提前致谢。
@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);
@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);
如上等等。
HTTP 协议不允许在同一个请求中使用 2 个 Content-Type。所以你必须选择:
- application/x-www-form-urlencoded
- multipart/form-data
您通过使用注释 @FormUrlEncoded
使用 application/x-www-form-urlencoded
为了发送图像,您必须将整个文件转换为文本(例如 base64)。
更好的方法是使用 multipart/form-data
来描述您的请求:
@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);
像这样调用 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);
希望这有效:)