无法使用翻新从API中检索键/值对

时间:2019-05-10 19:39:19

标签: java android api retrofit

我需要显示从API获得的键(货币)/值对,例如货币的简称和其值。不幸的是,当我试图获取我的Model类的任何值时,它显示为null。我该怎么解决?

我正在使用的API是https://api.exchangeratesapi.io/

我应该收到的数据看起来像这样

{
  "base": "EUR",
  "rates": {
    "BGN": 1.9558,
    "NZD": 1.7025,
    "ILS": 4.0004,
    "RUB": 73.3493,
    "CAD": 1.5132,
    "USD": 1.123,
    "PHP": 58.722,
    "CHF": 1.1378,
    "ZAR": 15.9762,
    "AUD": 1.6059,
    "JPY": 123.25,
    "TRY": 6.8837,
    "HKD": 8.8135,
    "MYR": 4.6781,
    "THB": 35.464,
    "HRK": 7.409,
    "NOK": 9.8193,
    "IDR": 16177.38,
    "DKK": 7.4658,
    "CZK": 25.732,
    "HUF": 323.52,
    "GBP": 0.8625,
    "MXN": 21.5589,
    "KRW": 1325.32,
    "ISK": 137.0,
    "SGD": 1.5305,
    "BRL": 4.4405,
    "PLN": 4.296,
    "INR": 78.6075,
    "RON": 4.7598,
    "CNY": 7.6628,
    "SEK": 10.8108
  },
  "date": "2019-05-10"
}

MainActivity:

public class MainActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.label);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.exchangeratesapi.io/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Api api = retrofit.create(Api.class);
        Call<Model.Rates> call = api.getRates();

        call.enqueue(new Callback<Model.Rates>() {
            @Override
            public void onResponse(Call<Model.Rates> call, Response<Model.Rates> response) {
                if (!response.isSuccessful()){
                    textView.setText(response.code());
                    return;
                }

                List<Model.Rates> ratesList = Collections.singletonList(response.body());

                for (Model.Rates model : ratesList){
                    String content = "";
                    content += "First "+model.getCAD()+"\n\n\n";
                    textView.append(content);
                }
            }

            @Override
            public void onFailure(Call<Model.Rates> call, Throwable t) {
                textView.setText(t.getMessage());
            }
        });
    }
}

模型类:

public class Model {

    private String date;

    public Rates rates;

    private String base;

    private String currencyNationality;
    private String currencyValue;

    public Model(String currencyNationality, String currencyValue) {
        this.currencyNationality = currencyNationality;
        this.currencyValue = currencyValue;
    }

    public String getCurrencyNationality() {
        return currencyNationality;
    }

    public void setCurrencyNationality(String currencyNationality) {
        this.currencyNationality = currencyNationality;
    }

    public String getCurrencyValue() {
        return currencyValue;
    }

    public void setCurrencyValue(String currencyValue) {
        this.currencyValue = currencyValue;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public Rates getRates() {
        return rates;
    }

    public void setRates(Rates rates) {
        this.rates = rates;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    class Rates {

        private String CHF;

        private String HRK;

        private String MXN;

        private String ZAR;

        private String INR;

        private String THB;

        private String CNY;

        private String AUD;

        private String ILS;

        private String KRW;

        private String JPY;

        private String PLN;

        private String GBP;

        private String IDR;

        private String HUF;

        private String PHP;

        private String TRY;

        private String RUB;

        private String HKD;

        private String ISK;

        private String DKK;

        private String CAD;

        private String USD;

        private String MYR;

        private String BGN;

        private String NOK;

        private String RON;

        private String SGD;

        private String CZK;

        private String SEK;

        private String NZD;

        private String BRL;

        public String getCHF() {
            return CHF;
        }

        public void setCHF(String CHF) {
            this.CHF = CHF;
        }

        public String getHRK() {
            return HRK;
        }

        public void setHRK(String HRK) {
            this.HRK = HRK;
        }

        public String getMXN() {
            return MXN;
        }

        public void setMXN(String MXN) {
            this.MXN = MXN;
        }

        public String getZAR() {
            return ZAR;
        }

        public void setZAR(String ZAR) {
            this.ZAR = ZAR;
        }

        public String getINR() {
            return INR;
        }

        public void setINR(String INR) {
            this.INR = INR;
        }

        public String getTHB() {
            return THB;
        }

        public void setTHB(String THB) {
            this.THB = THB;
        }

        public String getCNY() {
            return CNY;
        }

        public void setCNY(String CNY) {
            this.CNY = CNY;
        }

        public String getAUD() {
            return AUD;
        }

        public void setAUD(String AUD) {
            this.AUD = AUD;
        }

        public String getILS() {
            return ILS;
        }

        public void setILS(String ILS) {
            this.ILS = ILS;
        }

        public String getKRW() {
            return KRW;
        }

        public void setKRW(String KRW) {
            this.KRW = KRW;
        }

        public String getJPY() {
            return JPY;
        }

        public void setJPY(String JPY) {
            this.JPY = JPY;
        }

        public String getPLN() {
            return PLN;
        }

        public void setPLN(String PLN) {
            this.PLN = PLN;
        }

        public String getGBP() {
            return GBP;
        }

        public void setGBP(String GBP) {
            this.GBP = GBP;
        }

        public String getIDR() {
            return IDR;
        }

        public void setIDR(String IDR) {
            this.IDR = IDR;
        }

        public String getHUF() {
            return HUF;
        }

        public void setHUF(String HUF) {
            this.HUF = HUF;
        }

        public String getPHP() {
            return PHP;
        }

        public void setPHP(String PHP) {
            this.PHP = PHP;
        }

        public String getTRY() {
            return TRY;
        }

        public void setTRY(String TRY) {
            this.TRY = TRY;
        }

        public String getRUB() {
            return RUB;
        }

        public void setRUB(String RUB) {
            this.RUB = RUB;
        }

        public String getHKD() {
            return HKD;
        }

        public void setHKD(String HKD) {
            this.HKD = HKD;
        }

        public String getISK() {
            return ISK;
        }

        public void setISK(String ISK) {
            this.ISK = ISK;
        }

        public String getDKK() {
            return DKK;
        }

        public void setDKK(String DKK) {
            this.DKK = DKK;
        }

        public String getCAD() {
            return CAD;
        }

        public void setCAD(String CAD) {
            this.CAD = CAD;
        }

        public String getUSD() {
            return USD;
        }

        public void setUSD(String USD) {
            this.USD = USD;
        }

        public String getMYR() {
            return MYR;
        }

        public void setMYR(String MYR) {
            this.MYR = MYR;
        }

        public String getBGN() {
            return BGN;
        }

        public void setBGN(String BGN) {
            this.BGN = BGN;
        }

        public String getNOK() {
            return NOK;
        }

        public void setNOK(String NOK) {
            this.NOK = NOK;
        }

        public String getRON() {
            return RON;
        }

        public void setRON(String RON) {
            this.RON = RON;
        }

        public String getSGD() {
            return SGD;
        }

        public void setSGD(String SGD) {
            this.SGD = SGD;
        }

        public String getCZK() {
            return CZK;
        }

        public void setCZK(String CZK) {
            this.CZK = CZK;
        }

        public String getSEK() {
            return SEK;
        }

        public void setSEK(String SEK) {
            this.SEK = SEK;
        }

        public String getNZD() {
            return NZD;
        }

        public void setNZD(String NZD) {
            this.NZD = NZD;
        }

        public String getBRL() {
            return BRL;
        }

        public void setBRL(String BRL) {
            this.BRL = BRL;
        }

        List<Model> getCurrencyValuesList() {

            List<Model> currencyValues = new ArrayList<>();
            currencyValues.add(new Model("BGN", getAUD()));
            currencyValues.add(new Model("NZD", getNZD()));
            currencyValues.add(new Model("ILS", getILS()));
            currencyValues.add(new Model("RUB", getRUB()));
            currencyValues.add(new Model("CAD", getCAD()));
            currencyValues.add(new Model("USD", getUSD()));
            currencyValues.add(new Model("PHP", getPHP()));
            currencyValues.add(new Model("CHF", getCHF()));
            currencyValues.add(new Model("ZAR", getZAR()));
            currencyValues.add(new Model("AUD", getAUD()));
            currencyValues.add(new Model("JPY", getJPY()));
            currencyValues.add(new Model("TRY", getTRY()));
            currencyValues.add(new Model("HKD", getHKD()));
            currencyValues.add(new Model("MYR", getMYR()));
            currencyValues.add(new Model("THB", getTHB()));
            currencyValues.add(new Model("HRK", getHRK()));
            currencyValues.add(new Model("NOK", getNOK()));
            currencyValues.add(new Model("IDR", getIDR()));
            currencyValues.add(new Model("DKK", getDKK()));
            currencyValues.add(new Model("CZK", getCZK()));
            currencyValues.add(new Model("HUF", getHUF()));
            currencyValues.add(new Model("GBP", getBGN()));
            currencyValues.add(new Model("KRW", getKRW()));
            currencyValues.add(new Model("ISK", getISK()));
            currencyValues.add(new Model("SGD", getSGD()));
            currencyValues.add(new Model("BRL", getBRL()));
            currencyValues.add(new Model("PLN", getPLN()));
            currencyValues.add(new Model("INR", getINR()));
            currencyValues.add(new Model("RON", getRON()));
            currencyValues.add(new Model("CNY", getCNY()));
            currencyValues.add(new Model("SEK", getSEK()));
            return currencyValues;
        }
    }
}

Api类:

public interface Api {

    @GET("latest")
    Call<Model.Rates> getRates();
}

2 个答案:

答案 0 :(得分:1)

您的映射有点错误,我建议您采用这种方式。我创建了一个名为Currency的类,而不是您的模型类。您可以重命名或使用相同的

public class Currency {

@SerializedName("base") private String base;
@SerializedName("rates") private Map<String, Double> rates;

public String getBase() {
    return base;
}

public void setBase(String base) {
    this.base = base;
}

public Map<String, Double> getRates() {
    return rates;
}

public void setRates(Map<String, Double> rates) {
    this.rates = rates;
}

}

答案 1 :(得分:0)

映射相当像这样:

Model.java:

public class Model {

    @SerializedName("base")
    private String base;

    @SerializedName("rates")
    private Rates rates;

    @SerializedName("date")
    private String date;

    public String getBase() {
        return base;
    }

    public void setBase(String value) {
        this.base = value;
    }

    public Rates getRates() {
        return rates;
    }

    public void setRates(Rates value) {
        this.rates = value;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String value) {
        this.date = value;
    }    
}

Rates.java:

public class Rates {

    @SerializedName("BGN")
    private Double bGN;

    @SerializedName("NZD")
    private Double nZD;

    @SerializedName("ILS")
    private Double iLS;

    @SerializedName("RUB")
    private Double rUB;

    @SerializedName("CAD")
    private Double cAD;

    @SerializedName("USD")
    private Double uSD;

    @SerializedName("PHP")
    private Double pHP;

    @SerializedName("CHF")
    private Double cHF;

    @SerializedName("ZAR")
    private Double zAR;

    @SerializedName("AUD")
    private Double aUD;

    @SerializedName("JPY")
    private Double jPY;

    @SerializedName("TRY")
    private Double tRY;

    @SerializedName("HKD")
    private Double hKD;

    @SerializedName("MYR")
    private Double mYR;

    @SerializedName("THB")
    private Double tHB;

    @SerializedName("HRK")
    private Double hRK;

    @SerializedName("NOK")
    private Double nOK;

    @SerializedName("IDR")
    private Double iDR;

    @SerializedName("DKK")
    private Double dKK;

    @SerializedName("CZK")
    private Double cZK;

    @SerializedName("HUF")
    private Double hUF;

    @SerializedName("GBP")
    private Double gBP;

    @SerializedName("MXN")
    private Double mXN;

    @SerializedName("KRW")
    private Double kRW;

    @SerializedName("ISK")
    private Double iSK;

    @SerializedName("SGD")
    private Double sGD;

    @SerializedName("BRL")
    private Double bRL;

    @SerializedName("PLN")
    private Double pLN;

    @SerializedName("INR")
    private Double iNR;

    @SerializedName("RON")
    private Double rON;

    @SerializedName("CNY")
    private Double cNY;

    @SerializedName("SEK")
    private Double sEK;

    public Double getBGN() {
        return bGN;
    }

    public void setBGN(Double value) {
        this.bGN = value;
    }

    public Double getNZD() {
        return nZD;
    }

    public void setNZD(Double value) {
        this.nZD = value;
    }

    public Double getILS() {
        return iLS;
    }

    public void setILS(Double value) {
        this.iLS = value;
    }

    public Double getRUB() {
        return rUB;
    }

    public void setRUB(Double value) {
        this.rUB = value;
    }

    public Double getCAD() {
        return cAD;
    }

    public void setCAD(Double value) {
        this.cAD = value;
    }

    public Double getUSD() {
        return uSD;
    }

    public void setUSD(Double value) {
        this.uSD = value;
    }

    public Double getPHP() {
        return pHP;
    }

    public void setPHP(Double value) {
        this.pHP = value;
    }

    public Double getCHF() {
        return cHF;
    }

    public void setCHF(Double value) {
        this.cHF = value;
    }

    public Double getZAR() {
        return zAR;
    }

    public void setZAR(Double value) {
        this.zAR = value;
    }

    public Double getAUD() {
        return aUD;
    }

    public void setAUD(Double value) {
        this.aUD = value;
    }

    public Double getJPY() {
        return jPY;
    }

    public void setJPY(Double value) {
        this.jPY = value;
    }

    public Double getTRY() {
        return tRY;
    }

    public void setTRY(Double value) {
        this.tRY = value;
    }

    public Double getHKD() {
        return hKD;
    }

    public void setHKD(Double value) {
        this.hKD = value;
    }

    public Double getMYR() {
        return mYR;
    }

    public void setMYR(Double value) {
        this.mYR = value;
    }

    public Double getTHB() {
        return tHB;
    }

    public void setTHB(Double value) {
        this.tHB = value;
    }

    public Double getHRK() {
        return hRK;
    }

    public void setHRK(Double value) {
        this.hRK = value;
    }

    public Double getNOK() {
        return nOK;
    }

    public void setNOK(Double value) {
        this.nOK = value;
    }

    public Double getIDR() {
        return iDR;
    }

    public void setIDR(Double value) {
        this.iDR = value;
    }

    public Double getDKK() {
        return dKK;
    }

    public void setDKK(Double value) {
        this.dKK = value;
    }

    public Double getCZK() {
        return cZK;
    }

    public void setCZK(Double value) {
        this.cZK = value;
    }

    public Double getHUF() {
        return hUF;
    }

    public void setHUF(Double value) {
        this.hUF = value;
    }

    public Double getGBP() {
        return gBP;
    }

    public void setGBP(Double value) {
        this.gBP = value;
    }

    public Double getMXN() {
        return mXN;
    }

    public void setMXN(Double value) {
        this.mXN = value;
    }

    public Double getKRW() {
        return kRW;
    }

    public void setKRW(Double value) {
        this.kRW = value;
    }

    public Double getISK() {
        return iSK;
    }

    public void setISK(Double value) {
        this.iSK = value;
    }

    public Double getSGD() {
        return sGD;
    }

    public void setSGD(Double value) {
        this.sGD = value;
    }

    public Double getBRL() {
        return bRL;
    }

    public void setBRL(Double value) {
        this.bRL = value;
    }

    public Double getPLN() {
        return pLN;
    }

    public void setPLN(Double value) {
        this.pLN = value;
    }

    public Double getINR() {
        return iNR;
    }

    public void setINR(Double value) {
        this.iNR = value;
    }

    public Double getRON() {
        return rON;
    }

    public void setRON(Double value) {
        this.rON = value;
    }

    public Double getCNY() {
        return cNY;
    }

    public void setCNY(Double value) {
        this.cNY = value;
    }

    public Double getSEK() {
        return sEK;
    }

    public void setSEK(Double value) {
        this.sEK = value;
    }
}