3

私は改造オブジェクトフィールドの問題に直面しています。 例えば:私はオブジェクト名基準を持っており、それが20フィールド持って、私はAPIの呼び出しで4最初フィールドのみを送信しながら、API呼び出しを無視する必要があるすべてを休ませる必要はできません。以下は オブジェクトがRetrofit2のAPIコールでヌルフィールドを送信するのを防ぐ方法

は私の物である:今

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; 
} 

} 

対象にだからここ

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

の初期化中にすることができます基準からオブジェクトを利用して、すべてのように初期化されます休まされます総3パラメータNULL。私は、サーバーのログで確認したときに

今では初期化された値を含め、ここで検出したすべての値もブランク値と

を登場さ休まだから私は、オブジェクト からこのブランク値を防ぐことができますどのような任意の機能がありますJSONオブジェクトを削除できます。

+0

コンストラクタを作成し、必要な値を渡します。また、nullとしてリストを開始しないでください。 – Duke1992

+0

@ Duke1992:返信いただきありがとうございますが、ここで動的パラメータコンストラクタを作成する方法はありますか?最終的にどのパラメータを渡す必要がありますここでは固定されていないので、動的パラメータコンストラクタの解決策はありますか? –

+0

これを確認してくださいhttp://stackoverflow.com/a/19719701/3144174 – Duke1992

答えて

0

はたぶん、あなたはあなた自身のTypeAdapterを作成し、その後、Gsonに登録することができた後、あなたのGsonコンバータファクトリー

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();  
    } 
} 

の上に置いている探しているもの。

//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(); 
関連する問題