Cloudsight Api在Android平台上的图像上传提供空响应

时间:2017-05-16 10:44:06

标签: android cloudsight

我正在尝试在" https://api.cloudsightapi.com/image_requests" 上传图片 但是在请求调用之后,我将所有字段null变为响应.i.e状态,名称,令牌等。我正在使用retrofit 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);

我将所有字段都放到response.body null中。请帮忙。

2 个答案:

答案 0 :(得分:0)

像这样上传图片文件:

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

答案 1 :(得分:0)

CloudSight已停止接受MultipartBody.Part因此我们必须发送带有图像名称的RequestBody文件。

我已通过以下步骤解决了这个问题:

@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
  @Override
  public void addCorsMappings(CorsRegistry registry) {
      registry.addMapping("your cross origin url")
            .allowedOrigins("your cross origin host/url")
            .allowedHeaders("Access-Control-Allow-Origin", "*")
            .allowedHeaders("Access-Control-Allow-Headers", "Content-Type,x-requested-with").maxAge(20000)
            .allowCredentials(false)
            .allowedMethods("DELETE");
 }
}

// in your controller
@RequestMapping(value = '/{fileName:.+}', RequestMethod.DELETE)
void deleteFile(@PathVariable fileName) {
    // your custom logic
}

希望这会有所帮助。感谢。