java - 使用 Gson Response.body() 进行改造不起作用
问题描述
我正在尝试将数据解析到recyclerview,我在期待JSONArray / JSONObject时遇到了一些问题,我在一些帮助下修复了这些问题,但此时我有点迷失在Onresponse
原始文件中的操作 -generatePhonesList(response.body())
不起作用。
这是我的 json,我正在尝试解析以下数据array results[]
:
{
"success": true,
"metadata": {
"sort": "POPULARITY",
"total_products": 20,
"title": "Phones & Tablets",
"results": [
{
"sku": "1",
"name": "Samsung Galaxy S9",
"brand": "Samsung",
"max_saving_percentage": 30,
"price": 53996,
"special_price": 37990,
"image": "https://cdn2.gsmarena.com/vv/bigpic/samsung-galaxy-s9-.jpg",
"rating_average": 5
},
MainActivity(CALL 和 Recyclerview 创建):
GetPhoneDataService service = RetrofitInstance.getRetrofitInstance().create(GetPhoneDataService.class);
Call<APIReponse> call = service.getAllPhones();
call.enqueue(new Callback<APIReponse>() {
@Override
public void onResponse(Call<APIReponse> call, Response<APIReponse> response) {
generatePhonesList(response.body());
}
@Override
public void onFailure(Call<APIReponse> call, Throwable t) {
Log.e("eee" , "" + t.getMessage());
}
});
}
private void generatePhonesList(List<Result> phonesList){
recyclerView = findViewById(R.id.recyclerView);
adapter = new PhonesAdapter(phonesList,this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
这是在 jsonschema2pojo 中创建的 POJO 类:
public class APIReponse {
@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("metadata")
@Expose
private Metadata metadata;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public Metadata getMetadata() {
return metadata;
}
public void setMetadata(Metadata metadata) {
this.metadata = metadata;
}
}
2班
public class MetaData {
@SerializedName("sort")
@Expose
private String sort;
@SerializedName("total_products")
@Expose
private Integer totalProducts;
@SerializedName("title")
@Expose
private String title;
@SerializedName("results")
@Expose
private List<Result> results = null;
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
public Integer getTotalProducts() {
return totalProducts;
}
public void setTotalProducts(Integer totalProducts) {
this.totalProducts = totalProducts;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
}
3类:
public class Result {
@SerializedName("sku")
@Expose
private String sku;
@SerializedName("name")
@Expose
private String name;
@SerializedName("brand")
@Expose
private String brand;
@SerializedName("max_saving_percentage")
@Expose
private Integer maxSavingPercentage;
@SerializedName("price")
@Expose
private Integer price;
@SerializedName("special_price")
@Expose
private Integer specialPrice;
@SerializedName("image")
@Expose
private String image;
@SerializedName("rating_average")
@Expose
private Integer ratingAverage;
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getMaxSavingPercentage() {
return maxSavingPercentage;
}
public void setMaxSavingPercentage(Integer maxSavingPercentage) {
this.maxSavingPercentage = maxSavingPercentage;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Integer getSpecialPrice() {
return specialPrice;
}
public void setSpecialPrice(Integer specialPrice) {
this.specialPrice = specialPrice;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Integer getRatingAverage() {
return ratingAverage;
}
public void setRatingAverage(Integer ratingAverage) {
this.ratingAverage = ratingAverage;
}
}
解决方案
如果你抓紧response.body()
时间会给你上课APIResponse
。但你需要的是List<Result>
. 为此,请尝试response.body().getMetadata().getResults()
这应该会给你想要的输出。
推荐阅读
- python - SSEClient 无法使用 SSE 协议检索长流
- google-sheets-formula - 使用查询公式返回唯一行
- phpunit - phpunit 9 不打印错误堆栈跟踪
- android - 如何为 Google Play 商店签署我的颤振应用程序?
- php - 为什么会出现致命错误,尽管它已回显“连接成功”
- firebase - 具有顶级变量状态的firebase函数
- python - 无法在 Jupyter Notebook 中导入 Pyomo
- odoo - 如何在 linux mint 中正确安装 wkhtmltopdf?
- json - 如何将方法的返回类型更改为 JSON
- sql - 如何为 COUNT DISTINCT 运行 SUM() OVER PARTITION BY