3

개조 개체 필드에 문제가 있습니다. 예 : 객체 이름이 이고이며 필드가 있습니다. 아니요 먼저 네 개의 필드을 호출하고 나머지는 모두 api 호출 중에 무시해야합니다. 다음은 Retrofit2 API 호출시 개체가 null 필드를 보내는 것을 방지하는 방법

내 객체입니다 지금

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