我有以下职位要求-
[
{
"filters": {
"StockQuantity": {
"value": [0],
"cretiria": 0,
"type": 1
},
"Price": {
"value": [0],
"cretiria": 0,
"type": 1
}
}
}
]
事实是,您现在看到的StockQuantity
和Price
可以更改为许多具有不同名称的参数,而我不能仅使用这些名称创建一个类。
也就是说,三个参数value
,cretiria
和type
并非对于所有类型都改变。
我想创建一个具有“过滤器”类型的通用主体,我可以根据需要轻松添加任意数量的具有不同名称的过滤器,所有这些过滤器均具有3个参数-value
(通用类型列表),cretiria
(整数)和type
(int)
这是我目前拥有的...这很糟糕,不是通用的,而且很难编码-
private void getProducts() {
//stock variables
List<Integer> stockValue = new ArrayList<>();
stockValue.add(0);
int stockCriteria = 0;
int stockType = 1;
//price variables
List<Integer> priceValue = new ArrayList<>();
priceValue.add(0);
int priceCriteria = 0;
int priceType = 1;
//product name
List<String> productValue = new ArrayList<>();
int productCriteria = 0;
int productType = 5;
productValue.add("floral");
//setting the values of the json send as a body to the server
Filters filters = new Filters();
filters.setPrice(new Price(priceCriteria, priceType, priceValue));
filters.setStockQuantity(new StockQuantity(stockCriteria, stockType, stockValue));
//removed product name for test
//filters.setProductName(new ProductName(productCriteria, productType, productValue));
ProductAPIBodyRequest productAPIBodyRequest = new ProductAPIBodyRequest();
productAPIBodyRequest.setFilters(filters);
ProductAPIBodyRequest[] productAPIBodyRequests = {productAPIBodyRequest};
Call<JsonObject> products = marketApiCalls.getProducts(take, page, productAPIBodyRequests);
products.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (!response.isSuccessful()) {
return;
}
JsonObject body = response.body();
FullProductModel fullProductModel = gson.fromJson(body, FullProductModel.class);
for (int i = 0; i < fullProductModel.getData().size(); i++) {
com.twoverte.models.product.full_product.ResultData resultData = fullProductModel.getData().get(i).getResultData();
MiniProductModel model = new MiniProductModel(
resultData.getId(),
resultData.getImageUrl(),
resultData.getProductName(),
resultData.getShortDescription(),
resultData.getFullDescription(),
resultData.getVendorName(),
resultData.getVendorUrl(),
resultData.getUrl(),
resultData.getPictureList(),
resultData.getSku(),
resultData.getVendorImageUrl(),
resultData.getStockQuantity(),
resultData.getFavoritesCount(),
resultData.getPrice()
);
miniProductModelList.add(model);
}
productsAdapter.notifyDataSetChanged();
Log.d("allProducts", miniProductModelList.toString());
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
t.getMessage();
}
});
}
您是否考虑将过滤器转换为对象数组,并将“ StockQuantity”,“ Price”或其他键以及值,类型和条件字段一起移动到对象的主体中?所以像
{
"filters": [
{
"filter": "StockQuantity",
"value": [0],
"type": 1,
"criteria": 1
},
{
"filter": "Price",
"value": [0],
"type": 1,
"criteria": 1
}
]
}
从这里开始,您的Pojo将非常通用
public class Filters {
private List<Filter> filters;
public List<Filter> getFilters() {
return filters;
}
public void setFilters(List<Filter> filters) {
this.filters = filters;
}
private class Filter {
private String filter;
private List<Integer> value;
private int type;
private int criteria;
public String getFilter() {
return filter;
}
public void setFilter(String filter) {
this.filter = filter;
}
public List<Integer> getValue() {
return value;
}
public void setValue(List<Integer> value) {
this.value = value;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getCriteria() {
return criteria;
}
public void setCriteria(int criteria) {
this.criteria = criteria;
}
}
}
以这种方式进行操作意味着您可以通过将过滤器值更改为满足您需要的任何字符串来拥有所需的任何过滤器。