改造-上传图片

时间:2019-02-26 07:09:14

标签: android post retrofit image-uploading

我无法上传经过改造的图像。

我的改造服务界面:

@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);

问题是网络服务找不到带有键/名称photo的部件,并返回发送的图像为NULL。

This is the call on postman that works

2 个答案:

答案 0 :(得分:0)

请尝试使用此代码

@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);

答案 1 :(得分:0)

   @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);
        }