如何在改版Android中传递请求体中包含空值或空字符串的密钥 [英] How to pass a key with null value or an empty String in the request body in retrofit android

查看:40
本文介绍了如何在改版Android中传递请求体中包含空值或空字符串的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要传递密钥,即使没有值,因为密钥在服务器端是强制的。但Retrofit在发送请求时正在删除具有空值的密钥。如何使用Retrofit实现向服务器发送不带值的密钥?

请求正文如下:

{
        "first_name":"testlogin",
        "last_name":"lastname",
        "username":"testman", 
        "password":"test123", 
        "email":"tester@test.com", 
        "address1":"123+test+way",
        **"address2":"",**
        **"address3":"",**
        "postal_code":"75023",
        "country":1,
        **"state":""**
}

提前感谢。

推荐答案

尝试将其传递为:

"address2":null,
"address3":null,
"postal_code":"75023"
"country":1,
"state":null

由于您使用的是gson转换器,请尝试将gson创建为:

Gson gson = new GsonBuilder().serializeNulls().create();

发件人here

这篇关于如何在改版Android中传递请求体中包含空值或空字符串的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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