Reputation: 39
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.
Upvotes: 3
Views: 1698
Reputation: 313
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");
Upvotes: 1
Reputation: 374
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!
Upvotes: 1
Reputation: 13947
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 + "");
Upvotes: 0