Java code examples
The following code examples demonstrate the Java interaction with the API.
All examples also expect your
API key.
Example 1
Get the exchange rate between two currencies
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import java.net.URL;
URL url = new URL(
"http://jsonrates.com/get/?"+
"from=USD"+
"&to=EUR"
);
String data = IOUtils.toString(url);
JSONObject json = new JSONObject(data);
Double rate = json.getDouble("rate");
Example 2
Convert an amount from one into another currency
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import java.net.URL;
URL url = new URL(
"http://jsonrates.com/convert/?"+
"from=USD"+
"&to=EUR"+
"&amount=2.99"
);
String data = IOUtils.toString(url);
JSONObject json = new JSONObject(data);
Double amount = json.getDouble("amount");
Example 3
Get historical exchange rates between two currencies for three days
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import java.net.URL;
URL url = new URL(
"http://jsonrates.com/historical/?"+
"from=USD"+
"&to=EUR"+
"&dateStart=2014-06-25"+
"&dateEnd=2014-06-27"
);
String data = IOUtils.toString(url);
JSONObject rates = new JSONObject(data).getJSONObject("rates");
Iterator<?> keys = rates.keys();
while (keys.hasNext()) {
String date = (String) keys.next();
JSONObject values = rates.getJSONObject(date);
Double histrate = values.getDouble("rate");
}