Retrofit 2 Multipart POST请求向PHP发送额外的报价 [英] Retrofit 2 Multipart POST request sends extra quotes to PHP

查看:113
本文介绍了Retrofit 2 Multipart POST请求向PHP发送额外的报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Retrofit 2.0.1,Android应用程序中定义的API接口中有一个调用函数:

Using Retrofit 2.0.1, there is a call function in my API interface defined in Android App:

@Multipart
@POST("api.php")
Call<ResponseBody> doAPI(
  @Part("lang") String lang,
  @Part("file\"; filename=\"image.jpg") RequestBody file
);

我这样发送请求:

通话= service.doAPI("eng",imageFile);

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

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

where imageFile is a RequestBody created with a File object. The upload image part has no problem, while the @Part("lang") String lang part got extra quotes in server.

在PHP方面,其编写如下:

In PHP side, it is written as follow:

$lang = trim($_POST['lang']);

返回"eng".为什么在字符串周围加了一个双引号?

which returns "eng". Why there is an extra double quote surrounded the string?

我当然可以去除尾随双引号,但这很奇怪

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

推荐答案

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

For your issue, please use as the documentation

标量(基元,盒装和字符串): com.squareup.retrofit2:转换器标量

Scalars (primitives, boxed, and String): 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();

希望有帮助!

这篇关于Retrofit 2 Multipart POST请求向PHP发送额外的报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆