5

I am facing issue with retrofit object fields. e.g: I have object name criteria and it has 20 fields no I want to send only first 4 fields on API call and rest all should be ignore while api call.

Below is my object:

public class Criteria {

@SerializedName("mlsp_id")
@Expose
private String mlspId;
@SerializedName("listing_key")
@Expose
private String listing_key;
@SerializedName("limit")
@Expose
private String limit;
@SerializedName("add_to_log")
@Expose
private Integer addToLog;
@SerializedName("location")
@Expose
private String location;
@SerializedName("property_type")
@Expose
private List<String> propertyType = null;
@SerializedName("price")
@Expose
private Price price;
@SerializedName("beds")
@Expose
private Beds beds;
@SerializedName("baths")
@Expose
private Baths baths;
@SerializedName("floor_area")
@Expose
private FloorArea floorArea;
@SerializedName("lot_size")
@Expose
private LotSize lotSize;
@SerializedName("year_built")
@Expose
private YearBuilt yearBuilt;
@SerializedName("maintenance_fee")
@Expose
private MaintenanceFee maintenanceFee;
@SerializedName("dom")
@Expose
private String dom;
@SerializedName("keywords")
@Expose
private String keywords;
@SerializedName("exclude")
@Expose
private List<String> exclude = null;
@SerializedName("show_with")
@Expose
private List<String> showWith = null;
@SerializedName("map_boundary")
@Expose
private List<String> mapBoundary = null;
@SerializedName("sort_by")
@Expose
private String sortBy;
@SerializedName("sch_level")
@Expose
private String schLevel;
@SerializedName("sch_type")
@Expose
private String schType;
@SerializedName("sch_rating")
@Expose
private Integer schRating;
@SerializedName("listing_within_sch_radius")
@Expose
private Integer listingWithinSchRadius;
@SerializedName("sch_id")
@Expose
private String schId;
@SerializedName("sch_within_radius")
@Expose
private String schWithinRadius;
@SerializedName("listing_within_skytrain_station_radius")
@Expose
private Integer listingWithinSkytrainStationRadius;
@SerializedName("listing_within_skytrain_station_transit_minute")
@Expose
private Integer listingWithinSkytrainStationTransitMinute;
@SerializedName("skytrain_station_id")
@Expose
private Integer skytrainStationId;
@SerializedName("skytrain_station_within_radius")
@Expose
private Integer skytrainStationWithinRadius;

public String getListing_key() {
    return listing_key;
}

public void setListing_key(String listing_key) {
    this.listing_key = listing_key;
}

public String getLimit() {
    return limit;
}

public void setLimit(String limit) {
    this.limit = limit;
}

public String getMlspId() {
    return mlspId;
}

public void setMlspId(String mlspId) {
    this.mlspId = mlspId;
}

public Integer getAddToLog() {
    return addToLog;
}

public void setAddToLog(Integer addToLog) {
    this.addToLog = addToLog;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public List<String> getPropertyType() {
    return propertyType;
}

public void setPropertyType(List<String> propertyType) {
    this.propertyType = propertyType;
}

public Price getPrice() {
    return price;
}

public void setPrice(Price price) {
    this.price = price;
}

public Beds getBeds() {
    return beds;
}

public void setBeds(Beds beds) {
    this.beds = beds;
}

public Baths getBaths() {
    return baths;
}

public void setBaths(Baths baths) {
    this.baths = baths;
}

public FloorArea getFloorArea() {
    return floorArea;
}

public void setFloorArea(FloorArea floorArea) {
    this.floorArea = floorArea;
}

public LotSize getLotSize() {
    return lotSize;
}

public void setLotSize(LotSize lotSize) {
    this.lotSize = lotSize;
}

public YearBuilt getYearBuilt() {
    return yearBuilt;
}

public void setYearBuilt(YearBuilt yearBuilt) {
    this.yearBuilt = yearBuilt;
}

public MaintenanceFee getMaintenanceFee() {
    return maintenanceFee;
}

public void setMaintenanceFee(MaintenanceFee maintenanceFee) {
    this.maintenanceFee = maintenanceFee;
}

public String getDom() {
    return dom;
}

public void setDom(String dom) {
    this.dom = dom;
}

public String getKeywords() {
    return keywords;
}

public void setKeywords(String keywords) {
    this.keywords = keywords;
}

public List<String> getExclude() {
    return exclude;
}

public void setExclude(List<String> exclude) {
    this.exclude = exclude;
}

public List<String> getShowWith() {
    return showWith;
}

public void setShowWith(List<String> showWith) {
    this.showWith = showWith;
}

public List<String> getMapBoundary() {
    return mapBoundary;
}

public void setMapBoundary(List<String> mapBoundary) {
    this.mapBoundary = mapBoundary;
}

public String getSortBy() {
    return sortBy;
}

public void setSortBy(String sortBy) {
    this.sortBy = sortBy;
}

public String getSchLevel() {
    return schLevel;
}

public void setSchLevel(String schLevel) {
    this.schLevel = schLevel;
}

public String getSchType() {
    return schType;
}

public void setSchType(String schType) {
    this.schType = schType;
}

public Integer getSchRating() {
    return schRating;
}

public void setSchRating(Integer schRating) {
    this.schRating = schRating;
}

public Integer getListingWithinSchRadius() {
    return listingWithinSchRadius;
}

public void setListingWithinSchRadius(Integer listingWithinSchRadius) {
    this.listingWithinSchRadius = listingWithinSchRadius;
}

public String getSchId() {
    return schId;
}

public void setSchId(String schId) {
    this.schId = schId;
}

public String getSchWithinRadius() {
    return schWithinRadius;
}

public void setSchWithinRadius(String schWithinRadius) {
    this.schWithinRadius = schWithinRadius;
}

public Integer getListingWithinSkytrainStationRadius() {
    return listingWithinSkytrainStationRadius;
}

public void setListingWithinSkytrainStationRadius(Integer listingWithinSkytrainStationRadius) {
    this.listingWithinSkytrainStationRadius = listingWithinSkytrainStationRadius;
}

public Integer getListingWithinSkytrainStationTransitMinute() {
    return listingWithinSkytrainStationTransitMinute;
}

public void setListingWithinSkytrainStationTransitMinute(Integer listingWithinSkytrainStationTransitMinute) {
    this.listingWithinSkytrainStationTransitMinute = listingWithinSkytrainStationTransitMinute;
}

public Integer getSkytrainStationId() {
    return skytrainStationId;
}

public void setSkytrainStationId(Integer skytrainStationId) {
    this.skytrainStationId = skytrainStationId;
}

public Integer getSkytrainStationWithinRadius() {
    return skytrainStationWithinRadius;
}

public void setSkytrainStationWithinRadius(Integer skytrainStationWithinRadius) {
    this.skytrainStationWithinRadius = skytrainStationWithinRadius;
}

}

Now lets while initializing object

Criteria.setPrice(1000.00);
Criteria.setListingKey("Key20120");
Criteria.setMlspId("Id2000");

So here total 3 parameters will be utilized from Criteria object and rest all will be initialize as NULL.

Now when I checked in Server logs it detect here Including initialized values rest all values are also appeared with blank value

So How can I prevent this blank values from Object Is there any functionality like JSON Object where we can remove it.

Mohsin Khan
  • 139
  • 4
  • 13
  • Create a constructor and pass required values. And don't initiate list as null. – Duke1992 Mar 11 '17 at 12:51
  • @Duke1992: thanks for your reply, but how to create dynamic parameter constructor here? ultimately which parameter I have to pass Its not fixed here so Is there any solution for dynamic parameter constructor ? – Mohsin Khan Mar 11 '17 at 12:59
  • Check this http://stackoverflow.com/a/19719701/3144174 – Duke1992 Mar 11 '17 at 13:14
  • 2
    Just assign null value by default to all variable. – pRaNaY Mar 13 '17 at 11:23
  • @pRNaY: I did man but same thing happened , see when I checked logs I found only 3 values are added from whole object and rest all passed as blank, so do you any solution to prevent blank fields. – Mohsin Khan Mar 14 '17 at 07:16

1 Answers1

1

Maybe what are you looking for is creating your own TypeAdapter and put it on your Gson Converter Factory

public class CriteriaTypeAdapter extends TypeAdapter<Criteria>() {

  @Override
  public void write(JsonWriter out, Criteria value) throws IOException {
    out.beginObject();
    if (!Strings.isNullOrEmpty(value.getMlspId())) {
        out.name("mlsp_id");
        out.value(value.getMlspId());
    }

    if (!Strings.isNullOrEmpty(value.getListing_key())) {
        out.name("listing_key");
        out.value(value.getListing_key());
    }
    // ... and so on until last json field
    /* similar check for otherObject */         
    out.endObject();    
  }
}

After that you can then register it with Gson.

//you can registerTypeAdapter multiple times
Gson gson = new GsonBuilder()
              .registerTypeAdapter(Criteria.class,new CriteriaTypeAdapter())
              .create();

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL_SERVICE)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
Faruk
  • 5,438
  • 3
  • 30
  • 46