gpt4 book ai didi

java - 创建通用 API post body 请求?

转载 作者:行者123 更新时间:2023-12-02 09:17:14 25 4
gpt4 key购买 nike

我有以下帖子正文请求 -

[
{
"filters": {
"StockQuantity": {
"value": [0],
"cretiria": 0,
"type": 1
},
"Price": {
"value": [0],
"cretiria": 0,
"type": 1
}
}
}
]

问题是,您现在看到的 StockQuantityPrice 可以更改为许多具有不同名称的参数,而我不能只创建一个具有这些名称的类。

话虽如此,3 个参数 valuecretiriatype 不会针对所有类型而改变。

我想创建一个具有“过滤器”类型的通用主体,我可以根据需要轻松添加多个具有不同名称的过滤器,所有过滤器都有 3 个参数 - value(通用类型列表), cretiria (int) 和 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”、“价格”或其他键以及值、类型和条件字段一起移动到对象主体中?所以像

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

这样做意味着您可以通过将过滤器值更改为满足您需要的任何字符串来获得任何您想要的过滤器。

关于java - 创建通用 API post body 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58910716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com