【发布时间】:2016-04-28 03:00:31
【问题描述】:
使用Retrofit 2.0.1,在我的Android App中定义的API接口中有一个调用函数:
@Multipart
@POST("api.php")
Call<ResponseBody> doAPI(
@Part("lang") String lang,
@Part("file\"; filename=\"image.jpg") RequestBody file
);
我这样发送请求:
调用 call = service.doAPI("eng", imageFile);
其中imageFile 是使用File 对象创建的RequestBody。上传图片部分没有问题,而@Part("lang") String lang 部分在服务器中得到了额外的引号。
在PHP端是这样写的:
$lang = trim($_POST['lang']);
返回"eng"。为什么字符串周围多了一个双引号?
当然我可以去掉尾随和前导双引号,但这样做很奇怪
【问题讨论】:
-
如果你的项目使用Retrofit 2, IMO,你可以在stackoverflow.com/questions/36491096/…看到我的回答,请关注
Call<ResponseBody> postFile(@Part MultipartBody.Part file, @Part("description") RequestBody description); -
是的,使用 Retrofit 2。是否需要使用
MultipartBody.Part?因为RequestBody也适用于文件上传。有什么区别? -
尝试使用另一个转换器作为文档,
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars -
是的,我也在问题中发布了链接。可能是一个错误。
-
这是这个“bug”的正确解决方案:stackoverflow.com/a/36216137/2557258
标签: php android multipartform-data multipart retrofit2