我在解析值并在recyclerview
中显示时遇到了麻烦,使得Pojo
中的jsonschema2pojo
类位于OnFailure()
中,但是当我运行该应用程序时,它在队列{{ 1}},我尝试了很多事情,但是没有成功,我认为这可能与期望的jsonArray
/ jsonObject
事情有关?
谢谢。
我想获取数组results[]
内的值
Json响应如下:
"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
},
APIReponse pojo类:
public class APIReponse {
@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("metadata")
@Expose
private Metadata metadata;
MetaData pojo类:
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;
结果pojo类:
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;
这是改造API接口:
@GET("search/phone/page/1/")
Call<List<Result>> getallPhones();
改造调用方法:
Call<List<Result>> call = service.getallPhones();
call.enqueue(new Callback<List<Result>>() {
@Override
public void onResponse(Call<List<Result>> call,Response<List<Result>> response) {
generatePhonesList(response.body());
}
@Override
public void onFailure(Call<List<Result>> call,Throwable t) {
Toast.makeText(Mainactivity.this,"Something went wrong...Please try later!",Toast.LENGTH_SHORT).show();
}
});
}
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);
}