gpt4 book ai didi

java - 无法使用 Retrofit 从 API 检索键/值对

转载 作者:行者123 更新时间:2023-12-02 09:54:42 26 4
gpt4 key购买 nike

我需要显示从 API 获得的键(货币)/值对,例如货币的简称及其值。不幸的是,当我尝试获取模型类的任何值时,它显示为空。我该如何解决这个问题?

我正在使用的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"
}

主要 Activity :

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

最佳答案

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

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

}

关于java - 无法使用 Retrofit 从 API 检索键/值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com