【问题标题】:Retrofit - uploading an image改造 - 上传图片
【发布时间】:2019-07-19 16:14:06
【问题描述】:

我在上传带有改造的图像时遇到问题。

我的改造服务接口:

@Multipart
@Headers({
        "Content-Type: application/json"
})
@POST("upload_photo")
Call<ResponseBody> uploadPhoto(@Part MultipartBody.Part filePart);

调用服务:

MultipartBody.Part filePart = MultipartBody.Part.createFormData("photo", fileName, RequestBody.create(MediaType.parse("image/png"), bitmapByteArray));
Call<ResponseBody> call = service.uploadPhoto(filePart);

问题是 web 服务没有找到带有键/名称 photo 的部分,并返回发送的图像为 NULL。

This is the call on postman that works

【问题讨论】:

  • 从标题中删除内容类型然后尝试
  • @Nik 谢谢你.. 就是这样。

标签: android post retrofit image-uploading


【解决方案1】:

请试试这个代码

@Multipart
@POST("upload_photo")
Observable<ResponseBody> uploadPhoto(@Part("photo") RequestBody photo);
//////////////////////////////////////////////////////////////////////////////
//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);

service.uploadPhoto(body);

【讨论】:

    【解决方案2】:
       @Multipart
    @POST("upload_image")
    Call<UserProfileResponseBean> upload_image(@PartMap Map<String, RequestBody> part);
    
    
    
    if (path.isEmpty()) {
                upload_image = servicesInterface.upload_image(map);
            } else {
                File imagepath = new File(path);
                MultipartBody.Part body = MultipartBody.Part.createFormData("image", imagepath.getName(), RequestBody.create(MediaType.parse("image/*"), imagepath));
                upload_image = servicesInterface.upload_image(map, body);
            }
    

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 2018-12-08
      • 1970-01-01
      • 2020-05-13
      • 2021-05-08
      • 2015-09-16
      • 2015-04-05
      • 1970-01-01
      • 2018-12-20
      相关资源
      最近更新 更多