Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 server.

public class SelectedListModel implements Serializable {

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

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

  @Expose(serialize = false , deserialize = false)
  private String pName; //Have not to send to server

  @Expose(serialize = false , deserialize = false)
  private String pPrice; //Have not to send to server

  @Expose(serialize = false , deserialize = false)
  private String pImageUrl; //Have not to send to server
}

and because of that, I am getting 400 in my responses from the server. I used of @Expose(serialize = false, deserialize = false) in order to Ignore variables that have not to send to the server. But it doesn't work. Is there any way to do this or I have to create another model just for my server?

like image 308
Ehsan Avatar asked Aug 29 '17 05:08

Ehsan


People also ask

How do I ignore fields in retrofit?

To ignore the field elements simply add @Expose(deserialize = false, serialize = false) to your properties or none, and for (de)serialize your fields elements you can add the @Expose() annotations with empty values to your properties.

What is @body in retrofit?

The @Body annotation defines a single request body. interface Foo { @POST("/jayson") FooResponse postJson(@Body FooRequest body); } Since Retrofit uses Gson by default, the FooRequest instances will be serialized as JSON as the sole body of the request.

Is retrofit RESTful?

Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.


2 Answers

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

JAVA:

transient String name;

KOTLIN:

@Transient
var name: String
like image 93
Amir Raza Avatar answered Oct 05 '22 06:10

Amir Raza


Use transient keywork for that

public class SelectedListModel implements Serializable {

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

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

  //@Expose(serialize = false , deserialize = false)
  private transient String pName; //Have not to send to server

  //@Expose(serialize = false , deserialize = false)
  private transient String pPrice; //Have not to send to server

  //@Expose(serialize = false , deserialize = false)
  private transient String pImageUrl; //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.

like image 27
Pankaj Kumar Avatar answered Oct 05 '22 05:10

Pankaj Kumar