英文:
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("/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);
Afterwards this is how i call the above API interface method in a custom class:
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());
}
});
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("/companies/uploadImage")
Call<ServerResponse> companyUploadImage( @Part("company_id") RequestBody companyId, @Part MultipartBody.Part file, @Query("token") String token);
Maybe someone will find this post helpful.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论