android - 如何在 Retrofit 2 中上传图像文件

标签 android retrofit2

我有一张 postman 的图片,如下所示。如何在 Retrofit 2 中做同样的事情?

Enter image description here

我已经这样声明了接口(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/

相关文章:

android - Android 手机源代码/驱动程序的开放性

java - 如何在 Android 中验证 IAP?

android - 如何与 AlarmManager 一起启动通知?

android - 发布于改造

Android:Retrofit/Volley 是如何优于 AsyncTask 的

java - 使用 Retrofit 解析包含具有不同属性的对象的 JSON 数组

android - 如何自动更改对下一个列表项的关注

java - 本教程中的 @color/cream_dark 是什么?

android - 我如何使用 RxJava2 和 Retrofit2 进行 POST 请求?

android - 如何在 Kotlin android 改造中使用 enque 方法