文本在使用改造进行解析时返回 null

Text is returning null while parse with retrofit

我正在尝试解析一个简单的 json(如下所述),并将值设置为 textView。我正在使用改造和 GSON 转换器。但是在将值设置为 textviw 时,它返回 null。 我已经将值解析为 recyclerview,这很容易。但在简单的回应中,我可能犯了一些小错误,但找不到它。

不胜感激。

物品描述界面

public interface ItemDescriptionInterface {
    @GET("getProductDetailByProductId?ProductId=3")
    Call<JsonObject> ITEM_DESCRIPTION_RESPONSE_CALL();
}

Activity

私人无效 GetItemDescription() {

    Retrofit retrofit2 = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    ItemDescriptionInterface apiService = retrofit2.create(ItemDescriptionInterface.class);
    Call<JsonObject> jsonCall = apiService.ITEM_DESCRIPTION_RESPONSE_CALL();
    jsonCall.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {

            String jsonString = response.body().toString();
            Gson gson = new Gson();
            ItemDescriptionModel model = gson.fromJson(jsonString, ItemDescriptionModel.class);
            price.setText(model.getResult().getActualPrice());//Here its not getting 

        }
        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
            String msg = (t.getMessage() == null) ? "Login failed!" : t.getMessage();
            Log.d("descriptionofproduct", msg);
        }
    });

JSON 我收到的回复:

{
  "status": "Success",
  "response_code": 200,
  "result": [
    {
      "PId": "3",
      "ProductId": "3",
      "VendorId": "admin",
      "ProductName": "Golden Green",
      "ProductAlias": "golden-green-full-rim-",
      "MarketPrice": "500",
      "ActualPrice": "450",
      "PurchasePrice": "450",
      "Style": "3",
      "DefaultImage_url": "http:\/\/lensclone.tk\/test\.png"
    }
  ]
}

型号

public class ItemDescriptionModel {

@SerializedName("ActualPrice")
private String price;

@SerializedName("ProductDetails")
private String productDetails;

@SerializedName("DefaultImage_url")
private String imgurl;

@SerializedName("ProductName")
private String ProductName;

public ItemDescriptionModel(String price, String productDetails, String imgurl, String productName) {
    this.price = price;
    this.productDetails = productDetails;
    this.imgurl = imgurl;
    ProductName = productName;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getProductDetails() {
    return productDetails;
}

public void setProductDetails(String productDetails) {
    this.productDetails = productDetails;
}

public String getImgurl() {
    return imgurl;
}

public void setImgurl(String imgurl) {
    this.imgurl = imgurl;
}

public String getProductName() {
    return ProductName;
}

public void setProductName(String productName) {
    ProductName = productName;
}

}

我只想显示实际价格。

如果我遗漏了什么,请告诉我。

一旦您成功收到有效响应,您就没有正确解析 json。 model对象中不包含ActualPrice,这得从得到的json.

进一步解析

您需要为响应对象 (ItemDescriptionModel) 以及响应 json 中的任何嵌套对象创建模型 classes,例如此处的 result。您也可以使用JsontoJava等在线工具生成所需的模型class。

根据需要创建模型 class 后,

替换

price.setText(model.getPrice());

price.setText(model.getResult().getPrice());

改变这件事。

public interface ItemDescriptionInterface {
    @GET("getProductDetailByProductId?ProductId=3")
    Call<ItemDescriptionModel> ITEM_DESCRIPTION_RESPONSE_CALL();
}
        Call<ItemDescriptionModel> itemDescriptionModelCall= apiService.ITEM_DESCRIPTION_RESPONSE_CALL();
    itemDescriptionModelCall.enqueue(new Callback<ItemDescriptionModel>() {
        @Override
        public void onResponse(Call<ItemDescriptionModel> call, retrofit2.Response<ItemDescriptionModel> response) {
            if (response!=null && response.isSuccessful() && response.body()!=null){
                String prince=response.body().getPrice();
            }
        }

        @Override
        public void onFailure(Call<ItemDescriptionModel> call, Throwable t) {

        }
    });

ItemDescriptionInterface

public interface ItemDescriptionInterface {
    @GET("getProductDetailByProductId?ProductId=3")
    Call<ItemDescriptionModel> ITEM_DESCRIPTION_RESPONSE_CALL();
}

Activity

Retrofit retrofit2 = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ItemDescriptionInterface apiService = retrofit2.create(ItemDescriptionInterface.class);

    Call<ItemDescriptionModel> jsonCall = apiService.ITEM_DESCRIPTION_RESPONSE_CALL();
    jsonCall.enqueue(new Callback<ItemDescriptionModel>() {
        @Override
        public void onResponse(Call<ItemDescriptionModel> call, Response<ItemDescriptionModel> response) {

            ItemDescriptionModel model = (ItemDescriptionModel) response.body();
            price.setText(model.getResult().get(0).getActualPrice());
        }
    }

型号

public class ItemDescriptionModel {

@SerializedName("status")
private String status;

@SerializedName("response_code")
private String response_code;

@SerializedName("result")
private List<Results> result;

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getResponse_code() {
    return response_code;
}

public void setResponse_code(String response_code) {
    this.response_code = response_code;
}

public List<Results> getResult() {
    return result;
}

public void setResult(List<Results> result) {
    this.result = result;
}

private class Results {

    @SerializedName("PId")
    private String PId;

    @SerializedName("ProductId")
    private String ProductId;

    @SerializedName("VendorId")
    private String VendorId;

    @SerializedName("ProductName")
    private String ProductName;

    @SerializedName("ProductAlias")
    private String ProductAlias;

    @SerializedName("MarketPrice")
    private String MarketPrice;

    @SerializedName("ActualPrice")
    private String ActualPrice;

    @SerializedName("PurchasePrice")
    private String PurchasePrice;

    @SerializedName("Style")
    private String Style;

    @SerializedName("DefaultImage_url")
    private String DefaultImage_url;

    public String getPId() {
        return PId;
    }

    public void setPId(String PId) {
        this.PId = PId;
    }

    public String getProductId() {
        return ProductId;
    }

    public void setProductId(String productId) {
        ProductId = productId;
    }

    public String getVendorId() {
        return VendorId;
    }

    public void setVendorId(String vendorId) {
        VendorId = vendorId;
    }

    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String productName) {
        ProductName = productName;
    }

    public String getProductAlias() {
        return ProductAlias;
    }

    public void setProductAlias(String productAlias) {
        ProductAlias = productAlias;
    }

    public String getMarketPrice() {
        return MarketPrice;
    }

    public void setMarketPrice(String marketPrice) {
        MarketPrice = marketPrice;
    }

    public String getActualPrice() {
        return ActualPrice;
    }

    public void setActualPrice(String actualPrice) {
        ActualPrice = actualPrice;
    }

    public String getPurchasePrice() {
        return PurchasePrice;
    }

    public void setPurchasePrice(String purchasePrice) {
        PurchasePrice = purchasePrice;
    }

    public String getStyle() {
        return Style;
    }

    public void setStyle(String style) {
        Style = style;
    }

    public String getDefaultImage_url() {
        return DefaultImage_url;
    }

    public void setDefaultImage_url(String defaultImage_url) {
        DefaultImage_url = defaultImage_url;
    }
}}