【发布时间】:2018-09-22 07:27:37
【问题描述】:
我正在编写和android添加并使用retrofit2将数据发送到服务器。
我试图从正在序列化并发送到服务器的对象中排除一个字段。
该对象被命名为“Cheer”,当被发送到服务器时,我尝试从中排除字段 id。 iv'e 尝试使用 @Expose(false, false) 并解释 here 并尝试将字段设为私有,但仍将其发送到服务器。请参阅下面的 api、对象和调用。请注意,它有效,对象已添加到服务器,唯一的问题是,id 仍以 JSON 格式发送,我需要将其排除在外。
谢谢!!!
public class Cheer {
@Expose(deserialize = false, serialize = false)
private int id;
}
public interface CheersAPI {
String BASE_URL = "url:port";
@POST("/cheers")
Call<Cheer> AddCheer(@Body Cheer cheer);
}
cheersAPI.AddCheer(cheerToAdd).enqueue(new Callback<Cheer>(){
@Override
public void onResponse(Call<Cheer> call, Response<Cheer> response) {
Log.d("in the on response", "done creating a cheer");
}
@Override
public void onFailure(Call<Cheer> call, Throwable t) {
Log.d("failed", "failed to add a cheer here!");
}
});
【问题讨论】:
-
您使用的是哪个转换器?格森?
标签: java android post retrofit retrofit2