发送带有布尔值的模型 class 以改造有时不接受该布尔值的端点

Sending model class with Boolean values to retrofit endpoint that doesn't sometimes accept that boolean value

我想在发送请求时忽略一个布尔值,所有其他数据类型都可以正常工作,但布尔值默认设置为 false,即使端点不接受它也会发送 .. 注意:我不想在 GSON 中使用瞬态,因为其他端点接受该布尔值

public class UserModel implements Parcelable {

@SerializedName("agePrivate")
@Expose
private  boolean agePrivate;

@SerializedName("username")
@Expose
private String username;

@SerializedName("_id")
@Expose
private String _id;

@SerializedName("user_id")
@Expose
private String user_id;

@SerializedName("email")
@Expose
private String email;

@SerializedName("phone")
@Expose
private String phone;

@SerializedName("birthdayDate")
@Expose
private String birthdayDate;

@SerializedName("job")
@Expose
private String job;
@SerializedName("image")
@Expose
private String image;
@SerializedName("location")
@Expose
private LocationModel location;
private String freind_status;

您可以切换到不同的方法,例如通过从对象中提取值来按值发送数据。

@GET("/endpoint")
Call<List<model>>    getdata(@Field("location") String var, ....);

像这样创建调用对象时调用它

Call<List<model>> call = interface.getdata(obj.getLocation(),...);

那么做起来就很容易了

很简单,boolean原始类型的默认值始终是false,您应该将boolean替换为它的等效对象类型,即Boolean和它的默认值是 null 并且 null 将被 Gson 忽略。

所以替换:

@SerializedName("agePrivate")
@Expose
private  boolean agePrivate;

@SerializedName("agePrivate")
@Expose
private Boolean agePrivate;