我有一张 postman 的图片,如下所示。如何在 Retrofit 2 中做同样的事情?
我已经这样声明了接口(interface):
@Multipart
@POST("/api/Pharmarcy/UploadImage")
Call<ResponseBody> uploadPrescriptionImage(
@Query("accessToken") String token,
@Query("pharmarcyRequestId") int pharmacyRequestedId,
@Part MultipartBody.Part image);
最佳答案
@Multipart
@POST("user/updateprofile")
Observable<ResponseBody> updateProfile(@Part("user_id") RequestBody id,
@Part("full_name") RequestBody fullName,
@Part MultipartBody.Part image,
@Part("other") RequestBody other);
//pass it like this
File file = new File("/storage/emulated/0/Download/Corrections 6.jpg");
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("image", file.getName(), requestFile);
// add another part within the multipart request
RequestBody fullName =
RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name");
service.updateProfile(id, fullName, body, other);
看看我传递多部分和字符串参数的方式。希望对您有所帮助!
关于android - 如何在 Retrofit 2 中上传图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39953457/