Skip to content Skip to sidebar Skip to footer

How To Ignore Some Variables In Models Using For Retrofit

I am using Retrofit to send and receive requests to my server. I Have a model like below and I have to send it to my server but some variables in this model have not to send to the

Solution 1:

Use transient keywork for that

publicclassSelectedListModelimplementsSerializable {

  @SerializedName("p_id")
  @Expose
  private Long pId;

  @SerializedName("p_qty")
  @Expose
  private Double pQty;

  //@Expose(serialize = false , deserialize = false)privatetransientStringpName; //Have not to send to server//@Expose(serialize = false , deserialize = false)privatetransientStringpPrice; //Have not to send to server//@Expose(serialize = false , deserialize = false)privatetransientStringpImageUrl; //Have not to send to server
}

and no need to use @Expose(serialize = false , deserialize = false), into those fields which needed to be excluded.


Read Why does Java have transient fields? and Why use the `transient` keyword in java? for more details.

Solution 2:

You can use transient keyword to ignore fields when requesting api calls

JAVA:

transient String name;

KOTLIN:

@Transientvarname: String

Solution 3:

change your Retrofit adapter code like this (I hope you're using retrofit2)

Gsongson=newGsonBuilder()
     .excludeFieldsWithoutExposeAnnotation()
     .create();

Retrofitretrofit=newRetrofit.Builder()  
     .baseUrl(BASE_URL)
     .addConverterFactory(GsonConverterFactory.create(gson))
     .build();

Post a Comment for "How To Ignore Some Variables In Models Using For Retrofit"