我需要一点帮助,我已经没有足够的想法了,试图将该程序运行两天,但实际上我没有任何想法。所以,我想从程序到:

显示实际货币兑换
在另一个活动中,该程序应该有可能计算货币(比方说,我们有一个值为“ CZK”的字符串,因此该程序应从Model类货币值中获取,从而有可能计算货币,这就是问题所在,我无法获得这些值。实际上,我可以显示它,但是我想调用诸如getCZK();之类的方法,因为存在这些货币的实际值。为了准确起见,请尝试用其他方式解释它:假设基本货币是EUR,用户选择了期权,他想将EUR转换为CZK,所以程序现在应具有基于用户选择的字符串(在这种情况下,其字符串值为“ CZK”),程序应在其中搜索切换大小写(例如CZK)的大小写,当它找到它时,应该将getCZK();之类的方法传递给合适的变量,以使转换成为可能,我希望我对你们很好地解释了。

模型类

public class Model {

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

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

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

public String getBase() {
    return base;
}

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

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


阿皮

public interface Api {

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

@GET("latest")
Call<Model> getRatesByGivenCurrency(@Query("base") String base);
}


Java类

    public class exchange_currency extends AppCompatActivity {
    Api api;
    TextView currentCurrency, fromTV, toTV, receivedValue;
    EditText enteredValue;
    Spinner spinner;

    Button button;
    List<Model> list;

    String keyString;
    double valueDouble;

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

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

        spinner = (Spinner) findViewById(R.id.currencyNationalitiesSpinner);
        button = (Button) findViewById(R.id.btn);
        currentCurrency = (TextView) findViewById(R.id.currentValue);
        fromTV = (TextView) findViewById(R.id.actualCurrency);
        toTV = (TextView) findViewById(R.id.wantedCurrency);
        receivedValue = (TextView) findViewById(R.id.receivedValue);
        enteredValue = (EditText) findViewById(R.id.actualET);

        api = retrofit.create(Api.class);
        createRetrofit();
    }
    public void createRetrofit() {
        Call<Model> call = api.getRates();
        call.enqueue(new Callback<Model>() {
            @Override
            public void onResponse(Call<Model> call, Response<Model> response) {
                if (!response.isSuccessful()) {
                    Toast.makeText(exchange_currency.this, response.code(), Toast.LENGTH_LONG).show();
                }
                list = Collections.singletonList(response.body());

                for (Model model : list) {
                    Iterator<Map.Entry<String, Double>> entries = model.getRates().entrySet().iterator();
                    while (entries.hasNext()) {
                        Map.Entry entry = (Map.Entry) entries.next();
                        keyString = (String) entry.getKey();
                        valueDouble = (Double) entry.getValue();

                        toTV.append(keyString + "\n");
                        receivedValue.append(String.valueOf(valueDouble) + "\n");
                        toTV.append((CharSequence) model.getRates());
                    }
                }
            }
            @Override
            public void onFailure(Call<Model> call, Throwable t) {
                Toast.makeText(exchange_currency.this, t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }
}


我使用https://exchangeratesapi.io/的Api

来自API的示例性GET响应

{"base":"EUR","rates":{"BGN":1.9558,"NZD":1.7056,"ILS":4.0147,"RUB":73.2104,"CAD":1.5117,"USD":1.1226,"PHP":58.898,"CHF":1.1307,"ZAR":15.9746,"AUD":1.6162,"JPY":123.0,"TRY":6.7732,"HKD":8.8112,"MYR":4.6818,"THB":35.362,"HRK":7.4113,"NOK":9.799,"IDR":16199.12,"DKK":7.4691,"CZK":25.751,"HUF":324.23,"GBP":0.86723,"MXN":21.5178,"KRW":1333.2,"ISK":137.8,"SGD":1.5366,"BRL":4.4743,"PLN":4.3061,"INR":79.0375,"RON":4.7615,"CNY":7.7252,"SEK":10.779},"date":"2019-05-14"}

最佳答案

这些行在这里

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


相反应该是这样

@SerializedName("rates")
private Rates rates;


然后在onResponse中,您可以从响应中获取每个值,例如

Log.d("check", "CAD: "+response.body().getRates().getCAD());
Log.d("check", "GBP: "+response.body().getRates().getGBP());


哪个返回

2019-05-14 17:27:16.015 6816-6816/com.test.testing D/check: CAD: 1.5117
2019-05-14 17:27:16.015 6816-6816/com.test.testing D/check: GBP: 0.86723


这回答了你的问题了吗?

07-27 13:58