改进响应 return“NumberFormatexception:空字符串”

Retrofit response return " NumberFormatexception : Empty string"

其实我知道很多这样的问题,我已经在 google 上搜索过,但我找不到我的代码中的错误所在。

我试图发送请求以获取数据,但它运行了 onFailure 方法,该方法显示为 "NumberFormatException: Empty string" 并且数据无法显示在 recyclerview 中,尽管我也收到了我想要的 json 回复。

我收到了这样的回复:

{
    "kode":1,
    "pesan":"Barang ditemukan",
    "data":[
        {
            "Sat_1":"PT",
            "Sat_2":"",
            "Isi_2":"",
            "KdBrg":"280349191",
            "NmBrg":"SILICONE PACIFIER STEP 1A",
            "Stock_Akhir":"0",
            "Hrg":28000
        }
    ]
}

我的对象模型


public class ModelDataBarang {
    private int Isi_2;
    private int Isi_3;
    private int Isi_4;
    private String KdBrg;
    private String NmBrg;
    private String Sat_1;
    private String Sat_2;
    private String Sat_3;
    private String Sat_4;
    private String KdHrgList;
    private double Stock_Akhir;
    private double HrgJl11;

    public double getHrgJl11() {
        return HrgJl11;
    }

    public void setHrgJl11(double hrgJl11) {
        HrgJl11 = hrgJl11;
    }

    private String Hrg;

    public String getHrg() {
        return Hrg;
    }

    public void setHrg(String hrg) {
        Hrg = hrg;
    }

    public String getKdHrgList() {
        return KdHrgList;
    }

    public void setKdHrgList(String kdHrgList) {
        KdHrgList = kdHrgList;
    }

    public String getSat_3() {
        return this.Sat_3;
    }

    public void setSat_3(String sat_3) {
        this.Sat_3 = sat_3;
    }

    public String getSat_4() {
        return this.Sat_4;
    }

    public void setSat_4(String sat_4) {
        this.Sat_4 = sat_4;
    }

    public int getIsi_3() {
        return this.Isi_3;
    }

    public void setIsi_3(int isi_3) {
        this.Isi_3 = isi_3;
    }

    public int getIsi_4() {
        return this.Isi_4;
    }

    public void setIsi_4(int isi_4) {
        this.Isi_4 = isi_4;
    }

    public String getKdBrg() {
        return this.KdBrg;
    }

    public void setKdBrg(String kdBrg) {
        this.KdBrg = kdBrg;
    }

    public String getNmBrg() {
        return this.NmBrg;
    }

    public void setNmBrg(String nmBrg) {
        this.NmBrg = nmBrg;
    }

    public String getSat_1() {
        return this.Sat_1;
    }

    public void setSat_1(String sat_1) {
        this.Sat_1 = sat_1;
    }

    public String getSat_2() {
        return this.Sat_2;
    }

    public void setSat_2(String sat_2) {
        this.Sat_2 = sat_2;
    }

    public int getIsi_2() {
        return this.Isi_2;
    }

    public void setIsi_2(int isi_2) {
        this.Isi_2 = isi_2;
    }

    public double getStock_Akhir() {
        return this.Stock_Akhir;
    }

    public void setStock_Akhir(double stock_Akhir) {
        this.Stock_Akhir = stock_Akhir;
    }

    public String toString() {
        return this.NmBrg;
    }
}

我的 recyclerview 数据持有者

        ModelDataBarang modelBarangResto= listModel.get(position);

        holder.tvKdBrg.setText(modelBarangResto.getKdBrg());
        holder.tvNmBarang.setText(modelBarangResto.getNmBrg());
        holder.tvHrgBrg.setText(String.valueOf(modelBarangResto.getHrg()));

所有答案我都会感激

您的模型 class 值未根据您的回复定义。尝试检查和修改相应的 return 类型。您收到此错误是因为您在模型 class 中将 Isi_2 定义为 int 但作为响应您得到的是空字符串。

替换这个

 private int Isi_2;

有了这个

 private String Isi_2;