如何在Android Retrofit 2中上传照片?

时间:2019-02-10 14:29:29

标签: android retrofit

我无法将照片上传到服务器。 java.lang.IllegalArgumentException:@Field参数只能与表单编码一起使用。 (参数1)。请帮助我,如何解决或其他建议。 postman

@FormUrlEncoded
@Multipart
@POST("qq/api/xxxx")
Call<Custom> postCustom(@Field("Id") String Id,
   @Part MultipartBody.Part file,
   @Field("Status") String Status);

1 个答案:

答案 0 :(得分:0)

像这样更改您的请求界面

@Multipart
@POST("qq/api/xxxx")
Call<Custom> postCustom(
   @Part("Id") String Id,
   @Part MultipartBody.Part file,
   @Part("Status") String Status);

您不能在单个方法上同时使用@FormUrlEncoded@Multipart,因为HTTP请求只能有一个Content-Type@FormUrlEncoded@Multipart都是内容类型。

来自 Jake Wharton

  

您可能必须将FormUrlEncodedTypedOutput用作表单编码部分的@Part参数,然后自行构建。方法上的注释用于最外面的编码,在这种情况下,编码是多部分的。

参考

我还添加了这些问题参考,对您了解http请求方法中将要更改的内容确实很有帮助

相关问题