I need a little help, I'd already run out of ideas, tried to run this program out for two days, but I'm literally out of any ideas. So, I want from program to:
Display actual currency exchange In another activity, the program should have a possibility to calculate currencies (let's say, we have a String with value "CZK" so the program should get from Model class currency value, to get that possibility to calculate the currencies, and here's the problem, I can't get those values. Actually, I can display it, but I want to call methods like getCZK(); because there are the actual values of those currencies. To be more accurate, ill try to explain it in other way: Let's say, the base currency is EUR, user selected option, that he want converse EUR to CZK, so program should have a String based of user choice (in this case it's String with value "CZK") now, program should search in switch case for value such as CZK, when it will find it, it should pass method like getCZK(); to suitable variable, to make conversion possible. I hope I explained it well to you guys.
Model class
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;
}
}
Api
public interface Api {
@GET("latest")
Call<Model> getRates();
@GET("latest")
Call<Model> getRatesByGivenCurrency(@Query("base") String base);
}
Java class
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();
}
});
}
}
Api that I using https://exchangeratesapi.io/
exemplary GET response from API
{"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"}
these lines here
@SerializedName("rates")
private Map<String, Double> rates;
Should instead be like this
@SerializedName("rates")
private Rates rates;
Then in the onResponse you can get each of the values from the response like
Log.d("check", "CAD: "+response.body().getRates().getCAD());
Log.d("check", "GBP: "+response.body().getRates().getGBP());
Which returns
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
Does this answer your question?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.