我正在尝试上传"https://api.cloudsightapi.com/image_requests">上的图像但是在请求调用后,我将所有字段 null 变为响应 .即状态、名称、令牌等。我正在使用改造 2.0.1 进行请求调用。
代码:
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body =
MultipartBody.Part.createFormData("image_request[image]", file.getName(), requestFile);
String descriptionString = "en-US";
RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);
**API CALL:**
@Multipart
@POST("https://api.cloudsightapi.com/image_requests")
Call<FileUploadResponse> uploadPhoto(
@Header("Authorization") String authorisation,
@Part("image_request[locale]") RequestBody description,
@Part MultipartBody.Part file);
我正在将所有字段都放入响应.body null。请帮忙。
像这样上传图片文件:
File file = // initialize file here
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
Call<MyResponse> call = api.uploadPhoto(filePart);//u can send other parameters along with the filePart depending upon ur method signature
CloudSight 停止接受 MultipartBody.Part 因此,我们必须发送 RequestBody,即带有图像名称的文件。
我已经通过以下步骤解决了这个问题:
RequestBody requestFile = RequestBody.create(MediaType.parse("image*/"), file);
String descriptionString = "en-US";
RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);
apiInterface.uploadPhoto("CloudSight key",description, requestFile);
In retrofit api call:
@Multipart
@POST("https://api.cloudsightapi.com/image_requests")
Call<FileUploadResponse> uploadPhoto(
@Header("Authorization") String authorisation,
@Part("image_request[locale]") RequestBody description,
@Part("image_request[image]"; filename="file.jpg" " ) RequestBody file);
希望这有帮助。谢谢。