I have to receive historical currency data, but im not sure how i should do that
I assume i will know what I should do next, but I'm not sure how my model class should look like, there's no "constant" object that I can call like
"date":{ "some date" "some currency": some value }
I've received from API JSON data which look like this
{
"base": "EUR",
"rates": {
"2018-01-10": {
"USD": 1.1992
},
"2018-01-02": {
"USD": 1.2065
},
"2018-01-03": {
"USD": 1.2023
},
"2018-01-04": {
"USD": 1.2065
},
"2018-01-08": {
"USD": 1.1973
},
"2018-01-05": {
"USD": 1.2045
},
"2018-01-09": {
"USD": 1.1932
}
},
"end_at": "2018-01-10",
"start_at": "2018-01-01"
}
How I should name the variable which will enter "date" object, to get values from currency?
Here's api call
@GET("history?start_at=2018-01-01&end_at=2018-01-10&symbols=USD")
Call<Model> getMonthTest();
It is hardcoded for now, I just want to learn this.
Here's Retrofit call
HashMap<String, Double> currencyMap = new HashMap<>();
String key;
Double value;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_currency_detailed);
TEST();
}
public void TEST() {
String url = "https://api.exchangeratesapi.io/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<testModel> modelCall = api.getMonthTest("2018-01-01", "2018-02-01", "USD");
modelCall.enqueue(new Callback<testModel>() {
@Override
public void onResponse(Call<testModel> call, Response<testModel> response) {
if (!response.isSuccessful()) {
Log.i(TAG, String.valueOf(response.code()));
}
List<testModel> currencyList = Collections.singletonList(response.body());
for (testModel testModel : currencyList) {
currencyMap.put("USD", Double.valueOf(testModel.testRATES.getUsd()));
}
Iterator<Map.Entry<String, Double>> iterator = currencyMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
key = (String) entry.getKey();
value = (Double) entry.getValue();
}
}
@Override
public void onFailure(Call<testModel> call, Throwable t) {
}
});
}
}
POJO class
public class testModel {
String base;
@SerializedName("end_at")
String endAt;
@SerializedName("start_at")
String startAt;
Map<String, String> rates;
Rates testRATES;
public String getBase() {
return base;
}
public String getEndAt() {
return endAt;
}
public String getStartAt() {
return startAt;
}
public Map<String, String> getRates() {
return rates;
}
public class Rates{
@SerializedName("USD")
Double usd;
public Double getUsd() {
return usd;
}
}
}