2

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

1 Answers1

0

With retrofit, you can have variables in your query like this:

@GET("history")
Call<Model> getMonthTest(@Query("start_at") String startAt, @Query("end_at") String endAt, @Query("symbols") String symbol);

That would give you the result you want: history?start_at=startAt&end_at=endAt&symbols=symbol". startAt and endAt variables would be your date strings (properly formatted) and symbols would be USD in this case.

For more information if you want to have a dynamic path, headers, bodies and so on, check the documentation at https://square.github.io/retrofit/

Now when it comes to the model, here is a question that answers this: Parsing dynamic JSON to POJO with Retrofit

In your case, you would have something like:

class Response {
    String base;
    @SerializedName("end_at")
    String endAt;
    @SerializedName("start_at")
    String startAt;
    Map<String, Rate> rates;
}

class Rate {
    @SerializedName("USD")
    Double currency;
}

Note that Rate is set to parse USD only. I don't know how the response looks like if you request few symbols, I suppose it would be a List<Rate> but I am unsure. In that case Rate would have a Map<String, Double> variable instead.

Javier Mendonça
  • 1,970
  • 1
  • 17
  • 25
  • 1
    You provided partial answer to the question. Your answer solves passing parameters issue. What about the POJO to handle JSON data? Key-Value inside `rates` JSON object is dynamic. Hence, OP asked _"How I should name the variable which will enter "date" object, to get values from currency?"_ – Shashanth Jun 04 '19 at 03:57
  • 1
    @Shashanth that's right, totally forgot about that part of the question. Gonna update it. – Javier Mendonça Jun 04 '19 at 04:36
  • Can I see a hint how POJO class should look like? :) –  Jun 04 '19 at 14:37
  • You have it there in the answer, I called it `Response` @Aws – Javier Mendonça Jun 04 '19 at 14:51
  • I give up. Don't know how to make it... Tried to do somerthing familiar as I did with reading currencies and their values from this api, but that didn't work. Don't know what should I do here. I'll edit my question with java class that have Retrofit call –  Jun 05 '19 at 10:11