从Retrofit获取Gson列表给了我NPE

时间:2017-03-16 23:27:19

标签: android json gson retrofit

我需要从Json服务器下载数据。来自端点的json看起来像这样:

我已经从pojo创建了Java类:

-Photo
-ProductsResponse
-ResultProductResponse
-SelectedVariant
-Variant

以下是重要的课程:

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

}

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

}

这是我管理回复的方式

apiInterface.getProducts(productsQuery).enqueue(new Callback<ProductsResponse>() {
            @Override
            public void onResponse(Call<ProductsResponse> call, Response<ProductsResponse> response) {
                if (response.isSuccessful()) {
                    getProductsResponse(response);
                }
            }

这里是无效的getProductsResponse

void getProductsResponse(Response<ProductsResponse> response) {
        productsResponse = response.body();
        Log.i("TAG", productsResponse.getId());

        List<ResultProductResponse> resultList = productsResponse.getResultProductResponse();

        Log.i("TAG", resultList.size() + "");
    }

当我试图检查resultList的大小时,我得到NullPointerError。 从该列表中我只需要获得“名称”值。你能告诉我我做错了什么吗? 问候:)

2 个答案:

答案 0 :(得分:1)

从检查身体内部的响应开始.body() 在此行添加一个断点:getProductsResponse(response)并查看raw body

答案 1 :(得分:1)

更改

@SerializedName("resultProductResponse")

@SerializedName("result")

因为您的回复中没有resultProductResponse