3

I am trying to make a program which would exchange a given currency into a different one using live exchange rate. This is how I get data from exchangeratesapi.io:

    URL url = new URL("https://api.exchangeratesapi.io/latest?symbols=USD,GBP");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");

    BufferedReader in = new BufferedReader(new InputStreamReader(
        con.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
      System.out.println(inputLine);
    }

The code returns what is the current exchange rate ({"rates":{"USD":1.1029,"GBP":0.84183},"base":"EUR","date":"2020-01-30"}), but I dont how to get that 1.1029 into an equation.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
ozajasz15
  • 39
  • 6

3 Answers3

1

The "base" is the Euro. If you look on their github page here you will see in the README it says "Rates are quoted against the Euro by default. Quote against a different currency by setting the base parameter in your request." So what that means is if you put in 100, it's looking at the rate of 100 Euro. So to find another value it would be:

Amount * Exchange Rate = value for that Symbol

So in that example of 100 euro:

100 * 1.1029 = $110.29 USD.

EDIT: An update based on your comment. Here is how you would get the value, using the org.json library that you can find here.

  public static void main(String[] args) throws ParseException {
    String jsonString = "{ \"rates\" : { \"USD\" : 1.1029, \"GBP\" : 0.84183 }, \"base\" : \"EUR\", \"date\" : \"2020-01-30\" }";
    double rate = getExchangeRate(jsonString);
    System.out.println(rate);
  }


  public static double getExchangeRate(String jsonInput) {
    JSONObject object = new JSONObject(jsonInput);
    double rate = object.getJSONObject("rates").getDouble("USD");
    return rate;
  }

Output:

1.1029

If you're new to using this sort of stuff and JSON confuses you, I suggest you read up on some tutorials on how it works. A good start would be here.

I hope this helps!

djharten
  • 374
  • 1
  • 12
1

Use the JSONObject-import:

import org.json.JSONObject;

Small change to your code in the last few lines:

URL url = new URL("https://api.exchangeratesapi.io/latest?symbols=USD,GBP");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(
    con.getInputStream()));

String jsonText = readAll(in);
JSONObject yourData = new JSONObject(jsonText);

This is readAll:

private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
    }
    return sb.toString();
}

After that your data is a JSONObject and you can get the requested value like this:

double yourRate = yourData.getJSONObject("rates").getDouble("USD");
funky
  • 313
  • 3
  • 8
  • 14
  • Many thanks man, it definitely helps. The only issue I get is that I had an error: The method getFloat(String) is undefined for the type JSONObject – ozajasz15 Jan 30 '20 at 19:33
  • Ups. It should have been double. My fault! double yourRate = .... getDouble("USD"); (Changed the faulty float from me answer to double) – funky Jan 30 '20 at 19:35
  • Running an error: Error: Unable to initialize main class mainpackage1.CurrencyExchange Caused by: java.lang.NoClassDefFoundError: org/json/JSONException I added java-json.jar to my libraries, but still have this problem – ozajasz15 Jan 30 '20 at 20:21
  • This might help you: https://stackoverflow.com/questions/15951032/jsonobject-classnotfoundexception/15951178 – funky Jan 30 '20 at 20:25
  • had a few problems later, but now it all works. Many thanks man! – ozajasz15 Jan 30 '20 at 21:30
0
class Rate {
   public double USD;
   public double GBP;

}

class RateContainer{
    public Rate rates;
    public String base;
    public String date;

}

in your gradle file:

dependencies {
  implementation 'com.google.code.gson:gson:2.8.6'
}

Gson gson = Gson();
RateContainer container = gson.fromJson(inputLine,RateContainer.class);

Systen.out.println(container.rates.USD + "");
Lena Bru
  • 13,521
  • 11
  • 61
  • 126