[英]Getting List in Gson from Retrofit gives me NPE
I need to download data from Json server. 我需要从Json服务器下载数据。 The json from endpoint looks like that: 来自端点的json看起来像这样:
I have created Java Classes from pojo: 我从pojo创建了Java类:
-Photo
-ProductsResponse
-ResultProductResponse
-SelectedVariant
-Variant
Here are the important classes: 以下是重要的类:
public class ProductsResponse {
@SerializedName("resultProductResponse")
@Expose
private List<ResultProductResponse> resultProductResponse = null;
@SerializedName("id")
@Expose
private String id;
@SerializedName("jsonrpc")
@Expose
private String jsonrpc;
public List<ResultProductResponse> getResultProductResponse() {
return resultProductResponse;
}
public void setResultProductResponse(List<ResultProductResponse> resultProductResponse) {
this.resultProductResponse = resultProductResponse;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getJsonrpc() {
return jsonrpc;
}
public void setJsonrpc(String jsonrpc) {
this.jsonrpc = jsonrpc;
}
}
And: 和:
public class ResultProductResponse {
@SerializedName("description")
@Expose
private String description;
@SerializedName("base_price")
@Expose
private Double basePrice;
@SerializedName("variants")
@Expose
private List<Variant> variants = null;
@SerializedName("index_id")
@Expose
private Integer indexId;
@SerializedName("name")
@Expose
private String name;
@SerializedName("selected_variants")
@Expose
private List<SelectedVariant> selectedVariants = null;
@SerializedName("photo")
@Expose
private Photo photo;
@SerializedName("price")
@Expose
private Double price;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("unit")
@Expose
private String unit;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getBasePrice() {
return basePrice;
}
public void setBasePrice(Double basePrice) {
this.basePrice = basePrice;
}
public List<Variant> getVariants() {
return variants;
}
public void setVariants(List<Variant> variants) {
this.variants = variants;
}
public Integer getIndexId() {
return indexId;
}
public void setIndexId(Integer indexId) {
this.indexId = indexId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<SelectedVariant> getSelectedVariants() {
return selectedVariants;
}
public void setSelectedVariants(List<SelectedVariant> selectedVariants) {
this.selectedVariants = selectedVariants;
}
public Photo getPhoto() {
return photo;
}
public void setPhoto(Photo photo) {
this.photo = photo;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
This is how I manage response 这就是我管理回应的方式
apiInterface.getProducts(productsQuery).enqueue(new Callback<ProductsResponse>() {
@Override
public void onResponse(Call<ProductsResponse> call, Response<ProductsResponse> response) {
if (response.isSuccessful()) {
getProductsResponse(response);
}
}
And here is void getProductsResponse 这是无效的getProductsResponse
void getProductsResponse(Response<ProductsResponse> response) {
productsResponse = response.body();
Log.i("TAG", productsResponse.getId());
List<ResultProductResponse> resultList = productsResponse.getResultProductResponse();
Log.i("TAG", resultList.size() + "");
}
When i'm trying to check what is the size of resultList i get NullPointerError. 当我尝试检查resultList的大小时,我得到NullPointerError。 From that list I only need to get "name" value. 从该列表中,我只需要获取“名称”值。 Can you tell me what am I doing wrong? 你能告诉我我在做什么错吗? Regards :) 问候 :)
从检查主体的响应开始。body()在此行添加一个断点:getProductsResponse(response)并查看原始主体
Change 更改
@SerializedName("resultProductResponse")
to 至
@SerializedName("result")
since you have no resultProductResponse
in your response. 因为您的响应中没有resultProductResponse
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.