应用程序无法从改造中获取数据,它包含空值

时间:2019-05-01 17:46:33

标签: java android retrofit

从“ onResponse”接收数据时出现问题,我已经检查了代码,它是200,所以应该可以工作。失败在哪里?我不确定接口命名是否正确。这是我的代码

MainActivity

public class MainActivity extends AppCompatActivity {
private TextView textViewResult;

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

    textViewResult = findViewById(R.id.text_view_result);

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

    CurrencyExchangeService currencyInterface = retrofit.create(CurrencyExchangeService.class);
    Call<Currency> call = currencyInterface.getCurrency();
    call.enqueue(new Callback<Currency>() {
        @Override
        public void onResponse(Call<Currency> call, Response<Currency> response) {
            if (response.isSuccessful()) {
                textViewResult.setText("CODE: " + response.code());
            }

            List<Currency> list = Collections.singletonList(response.body());

            for (Currency currency : list) {
                String content = "";
                content += "a: " + currency.getEUR() + "\n";
                content += "b: " + currency.getCHF() + "\n";
                content += "c: " + currency.getGBP() + "\n\n";

                textViewResult.append(content);
            }
        }

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

货币

public class Currency {

 private String name;
 private double rate;

public Currency(String name, double rate) {
    this.name = name;
    this.rate = rate;
}

public String getName() {
    return name;
}

public double getRate() {
    return rate;
 }
}

CurrencyExchangeService

public interface CurrencyExchangeService {
@GET("latest")
Call<Currency> getCurrency();
}

我的程序在avd上运行,我可以获取的值是基数和日期(这在Currency Exchange类的顶部已提到),但是当我想获取某种货币的值时,它将显示为0.0

/**
 * base : EUR
 * rates : {"BGN":1.9558,"NZD":1.6805,"ILS":4.0448,"RUB":72.2099,"CAD":1.5075,"USD":1.1218,"PHP":58.125,"CHF":1.1437,"ZAR":16.0224,"AUD":1.5911,"JPY":124.93,"TRY":6.6913,"HKD":8.8007,"MYR":4.6314,"THB":35.802,"HRK":7.413,"NOK":9.6678,"IDR":15953.68,"DKK":7.4646,"CZK":25.659,"HUF":322.97,"GBP":0.86248,"MXN":21.2829,"KRW":1308.01,"ISK":136.2,"SGD":1.5263,"BRL":4.405,"PLN":4.2868,"INR":78.0615,"RON":4.7596,"CNY":7.5541,"SEK":10.635}
 * date : 2019-04-30
 */

这就是我上面所写的

应该是什么样(从api网站复制)

{
"base": "EUR",
"date": "2018-04-08",
"rates": {
"CAD": 1.565,
"CHF": 1.1798,
"GBP": 0.87295,
"SEK": 10.2983,
"EUR": 1.092,
"USD": 1.2234,
...
  }
}

*我添加了另一个包含所有货币值的类,但是仍然无法将这些值添加到我的应用中

CurrencyExchange

public class CurrencyExchange {

/**
 * base : EUR
 * rates : {"BGN":1.9558,"NZD":1.6805,"ILS":4.0448,"RUB":72.2099,"CAD":1.5075,"USD":1.1218,"PHP":58.125,"CHF":1.1437,"ZAR":16.0224,"AUD":1.5911,"JPY":124.93,"TRY":6.6913,"HKD":8.8007,"MYR":4.6314,"THB":35.802,"HRK":7.413,"NOK":9.6678,"IDR":15953.68,"DKK":7.4646,"CZK":25.659,"HUF":322.97,"GBP":0.86248,"MXN":21.2829,"KRW":1308.01,"ISK":136.2,"SGD":1.5263,"BRL":4.405,"PLN":4.2868,"INR":78.0615,"RON":4.7596,"CNY":7.5541,"SEK":10.635}
 * date : 2019-04-30
 */

private String base;
private RatesBean rates;
private String date;
private double USD;

public List<Currency> getCurrencyList(){
    List<Currency> currencyList = new ArrayList<>();
    currencyList.add(new Currency("AUD",rates.getAUD()));
    currencyList.add(new Currency("BGN",rates.getBGN()));
    currencyList.add(new Currency("USD",rates.getUSD()));
    return currencyList;
}

public double getUSD() {
    return USD;
}

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


public String getBase() {
    return base;
}

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

public RatesBean getRates() {
    return rates;
}

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

public String getDate() {
    return date;
}

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

public static class RatesBean {
    /**
     * BGN : 1.9558
     * NZD : 1.6805
     * ILS : 4.0448
     * RUB : 72.2099
     * CAD : 1.5075
     * USD : 1.1218
     * PHP : 58.125
     * CHF : 1.1437
     * ZAR : 16.0224
     * AUD : 1.5911
     * JPY : 124.93
     * TRY : 6.6913
     * HKD : 8.8007
     * MYR : 4.6314
     * THB : 35.802
     * HRK : 7.413
     * NOK : 9.6678
     * IDR : 15953.68
     * DKK : 7.4646
     * CZK : 25.659
     * HUF : 322.97
     * GBP : 0.86248
     * MXN : 21.2829
     * KRW : 1308.01
     * ISK : 136.2
     * SGD : 1.5263
     * BRL : 4.405
     * PLN : 4.2868
     * INR : 78.0615
     * RON : 4.7596
     * CNY : 7.5541
     * SEK : 10.635
     */

    private double BGN;
    private double NZD;
    private double ILS;
    private double RUB;
    private double CAD;
    private double USD;
    private double PHP;
    private double CHF;
    private double ZAR;
    private double AUD;
    private double JPY;
    private double TRY;
    private double HKD;
    private double MYR;
    private double THB;
    private double HRK;
    private double NOK;
    private double IDR;
    private double DKK;
    private double CZK;
    private double HUF;
    private double GBP;
    private double MXN;
    private double KRW;
    private double ISK;
    private double SGD;
    private double BRL;
    private double PLN;
    private double INR;
    private double RON;
    private double CNY;
    private double SEK;

    public double getBGN() {
        return BGN;
    }

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

    public double getNZD() {
        return NZD;
    }

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

    public double getILS() {
        return ILS;
    }

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

    public double getRUB() {
        return RUB;
    }

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

    public double getCAD() {
        return CAD;
    }

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

    public double getUSD() {
        return USD;
    }

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

    public double getPHP() {
        return PHP;
    }

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

    public double getCHF() {
        return CHF;
    }

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

    public double getZAR() {
        return ZAR;
    }

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

    public double getAUD() {
        return AUD;
    }

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

    public double getJPY() {
        return JPY;
    }

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

    public double getTRY() {
        return TRY;
    }

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

    public double getHKD() {
        return HKD;
    }

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

    public double getMYR() {
        return MYR;
    }

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

    public double getTHB() {
        return THB;
    }

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

    public double getHRK() {
        return HRK;
    }

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

    public double getNOK() {
        return NOK;
    }

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

    public double getIDR() {
        return IDR;
    }

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

    public double getDKK() {
        return DKK;
    }

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

    public double getCZK() {
        return CZK;
    }

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

    public double getHUF() {
        return HUF;
    }

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

    public double getGBP() {
        return GBP;
    }

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

    public double getMXN() {
        return MXN;
    }

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

    public double getKRW() {
        return KRW;
    }

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

    public double getISK() {
        return ISK;
    }

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

    public double getSGD() {
        return SGD;
    }

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

    public double getBRL() {
        return BRL;
    }

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

    public double getPLN() {
        return PLN;
    }

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

    public double getINR() {
        return INR;
    }

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

    public double getRON() {
        return RON;
    }

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

    public double getCNY() {
        return CNY;
    }

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

    public double getSEK() {
        return SEK;
    }

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

1 个答案:

答案 0 :(得分:0)

有很多问题。

  1. 您的POJO类和您的JSON不同
  2. 您正在JSON对象中获取一个JSON对象,但是您尝试以User的身份获取。
  3. 您正在以自己的货币在JSON中获得user_id=id值。但是您将变量声明为List<Currency>

编辑2:

界面:

double

api调用:

String