问题描述
我正在开发一个Android应用,该应用使用Android Retrofit发送JSON(它将POJO类转换为JSON).它工作正常,但是在发送JSON时,我需要忽略POJO类中的一个元素.
I am developing an Android App which is sending a JSON using Android Retrofit (it converts a POJO class in a JSON). It is working fine, but I need to ignore in the sending of JSON one element from the POJO class.
有人知道任何Android Retrofit批注吗?
Does anyone know any Android Retrofit annotation?
示例
POJO类:
public class sendingPojo
{
long id;
String text1;
String text2;//--> I want to ignore that in the JSON
getId(){return id;}
setId(long id){
this.id = id;
}
getText1(){return text1;}
setText1(String text1){
this.text1 = text1;
}
getText2(){return text2;}
setText2(String text2){
this.text2 = text2;
}
}
接口发送方ApiClass
public interface SvcApi {
@POST(SENDINGPOJO_SVC_PATH)
public sendingPojo addsendingPojo(@Body sendingPojo sp);
}
有什么想法如何忽略 text2 ?
推荐答案
如果您不想使用new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
,我发现了另一种解决方案.
I found an alternative solution if you don't want to use new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
.
只需在要忽略的变量中包含transient
.
Just including transient
in the variable I need to ignore.
因此,POJO类终于实现了:
So, the POJO class finally:
public class sendingPojo {
long id;
String text1;
transient String text2;//--> I want to ignore that in the JSON
getId() {
return id;
}
setId(long id) {
this.id = id;
}
getText1() {
return text1;
}
setText1(String text1) {
this.text1 = text1;
}
getText2() {
return text2;
}
setText2(String text2) {
this.text2 = text2;
}
}
我希望对您有帮助
这篇关于如何在Android Retrofit中忽略JSON元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!