Retrofit的Multipart表单数据中的POST值为空。

huangapple go评论110阅读模式
英文:

Retrofit Post Multipart form data values are empty

问题

我正在尝试通过Retrofit在Android中发布一些多部分表单数据,以便将其发送到Web服务。Web服务的API要求将以下参数作为多部分表单数据的字段传递:

字段         类型
company_id   text/plain
image        image/*

它还希望将授权令牌作为查询字符串参数传递。

因此,我已经像这样定义了我的API接口调用:

@Multipart
@POST("/companies/uploadImage")
@Headers({
"Content-Type: multipart/form-data",
"Accept: application/json"
})
Call<ServerResponse> companyUploadImage(@Part("company_id") RequestBody companyId, @Part MultipartBody.Part file, @Query("token") String token);

然后,这是我如何在自定义类中调用上述API接口方法的方式:

RequestBody companyId = RequestBody.create(MediaType.parse("text/plain"), LocalStorage.getInstance().getCompanyId());
File file = new File(postPath);

MultipartBody.Part image = MultipartBody.Part.createFormData("image", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
                                       
Call<ServerResponse> uploadProfileImage = router.companyUploadImage(companyId, image, token);

uploadProfileImage.enqueue(new Callback<ServerResponse>() {
  @Override
  public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {

       Log.e("Upload Profile Image: ", response.body().getMessage());
}

@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
       Log.e("Upload Profile Image Error: ", t.getMessage());
  }
});

请求成功发送到服务器,因此不会出现网络异常,但是服务器端收到的多部分表单值,company_id和image,为空。有任何想法吗?谢谢!

英文:

I am trying to post some multipart form data via Retrofit in Android to a web service.
The web services's API expects the following parameters to be passed as field of a multipart form data:

Name         Type
company_id   text/plain
image        image/*

It also expects an authorization token to be passed as a query string parameter.

So i have defined my API interface call like this:

@Multipart
@POST(&quot;/companies/uploadImage&quot;)
@Headers({
&quot;Content-Type: multipart/form-data&quot;,
&quot;Accept: application/json&quot;
})
Call&lt;ServerResponse&gt; companyUploadImage( @Part(&quot;company_id&quot;) RequestBody companyId, @Part MultipartBody.Part file, @Query(&quot;token&quot;) String token);

Afterwards this is how i call the above API interface method in a custom class:

RequestBody companyId = RequestBody.create(MediaType.parse(&quot;text/plain&quot;), LocalStorage.getInstance().getCompanyId());
File file = new File(postPath);

MultipartBody.Part image = MultipartBody.Part.createFormData(&quot;image&quot;, file.getName(), RequestBody.create(MediaType.parse(&quot;image/*&quot;), file));
                                       
Call&lt;ServerResponse&gt; uploadProfileImage = router.companyUploadImage(companyId, image, token);

uploadProfileImage.enqueue(new Callback&lt;ServerResponse&gt;() {
  @Override
  public void onResponse(Call&lt;ServerResponse&gt; call, Response&lt;ServerResponse&gt; response) {

       Log.e(&quot;Upload Profile Image: &quot;, response.body().getMessage());
}

@Override
public void onFailure(Call&lt;ServerResponse&gt; call, Throwable t) {
       Log.e(&quot;Upload Profile Image Error: &quot;, t.getMessage());
  }
});

The request is sent successfully to the server, so no networking exceptions occur, however the multipart form values, company_id and image, are received empty on the server side

Any idea?
Thank you!

答案1

得分: 0

我成功解决了这个问题。看起来解决方案就是从API接口方法中移除@Header注解。所以现在它看起来是这样的:

@Multipart
@POST("/companies/uploadImage")
Call<ServerResponse> companyUploadImage(@Part("company_id") RequestBody companyId, @Part MultipartBody.Part file, @Query("token") String token);

也许有人会发现这篇帖子有帮助。

英文:

I managed to solve the issue. It seems that the solution was as simple as removing the @Header annotation from the API interface method. So now it looks like this:

@Multipart
@POST(&quot;/companies/uploadImage&quot;)
Call&lt;ServerResponse&gt; companyUploadImage( @Part(&quot;company_id&quot;) RequestBody companyId, @Part MultipartBody.Part file, @Query(&quot;token&quot;) String token);

Maybe someone will find this post helpful.

huangapple
  • 本文由 发表于 2020年8月28日 22:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63635708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定