I have the following post body request -
[ { "filters": { "StockQuantity": { "value": [0], "cretiria": 0, "type": 1 }, "Price": { "value": [0], "cretiria": 0, "type": 1 } } } ] The thing is that what you see now as StockQuantity and Price can change to alot of parameters with different names and I can't just create a class with these names.
That being sayed, the 3 parameters value, cretiria and type do not change for all types.
I want to create a generic body that has a "filters" type that I can easily add as many filters with different names as needed, all having 3 parameters - value (generic type list), cretiria (int) and type (int)
Here is currently what I have...which is bad, not generic and very hard coded -
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(); } }); }
JsonAnyGetterfor "additional unspecified properties".