Retrofit 2 Multipart POST请求向PHP发送额外的引号

Rap*_*tor 11 php android multipartform-data multipart retrofit2

使用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
);
Run Code Online (Sandbox Code Playgroud)

我发送这样的请求:

Call call = service.doAPI("eng",imageFile);

其中imageFile是一个RequestBody与创建File对象.上传图片部分没有问题,而@Part("lang") String lang部分在服务器中得到额外的报价.

在PHP方面,它写如下:

$lang = trim($_POST['lang']);
Run Code Online (Sandbox Code Playgroud)

返回"eng".为什么在字符串周围有一个额外的双引号?

当然我可以删除尾随和引导双引号,但这样做很奇怪


相关问题:https://github.com/square/retrofit/issues/1210

BNK*_*BNK 16

对于您的问题,请使用作为文档

标量(原语,盒装和字符串):com.squareup.retrofit2:converter-scalars

所以,添加compile 'com.squareup.retrofit2:converter-scalars:2.0.1'build.gradle文件中

然后...

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(API_URL_BASE)
    .addConverterFactory(ScalarsConverterFactory.create())
    //.addConverterFactory(GsonConverterFactory.create())
    .build();
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!


小智 6

使用RequestBody您的所有参数。请通过下面的代码!

File file = new File(imagePath);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part imageFileBody = MultipartBody.Part.createFormData("media", file.getName(), requestBody);
RequestBody id = RequestBody.create(MediaType.parse("text/plain"),addOfferRequest.getCar_id());
ApiCallback.MyCall<BaseResponse> myCall = apiRequest.editOfferImage(imageFileBody,id);
Run Code Online (Sandbox Code Playgroud)

使用RequestBodyclass Retrofit代替String

@Multipart
@POST(ApiURL)
ApiCallback.MyCall<BaseResponse> editOfferImage(@Part MultipartBody.Part imageFile,@Part("id") RequestBody id);
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

3544 次

最近记录:

6 年 前