I would use http://xurrency.com/api/ to retrieve up-to-date exchange rates, you have to pay 29.99 for this though if you are querying it more than 10 times per day. The documentation part of the page explains how to use the API. E.g. to retrieve the US to EURO exchange you use http://xurrency.com/api/usd/eur/1.
If you enter this into your browser and view the source, you will see the response retrieved. The response is in JSON.
You will need to use something like GSON to convert the JSON data into a Java object. The code to do this should be pretty simple, something like:
URL url = new URL("http://xurrency.com/api/usd/eur/1");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String jsonObject = "";
String line;
while ((line = in.readLine()) != null)
jsonObject += line;
in.close();
Gson gson = new Gson();
ExchangeRate = gson.fromJson(jsonObject, ExchangeRate.class);
The ExchnageRate class would be a custom class designed to encapsulate the data retrieved from this JSON API.
You can make the above code work with the link you are using. Try the following code:
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.google.com/ig/calculator?hl=en&q=1USD%3D%3FCAD");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String jsonObject = "";
String line;
while ((line = in.readLine()) != null)
jsonObject += line;
in.close();
System.out.println(jsonObject);
Gson gson = new Gson();
GoogleCurrency another = gson.fromJson(jsonObject, GoogleCurrency.class);
another.print();
}
}
public class GoogleCurrency {
private String lhs;
private String rhs;
private String error;
private String icc;
public GoogleCurrency() {
lhs = "0 U.S Dollar";
rhs = "0 U.S Dollar";
error = "true";
icc = "false";
}
public GoogleCurrency(String lhs,String rhs,String error,String icc) {
this.lhs = lhs;
this.rhs = rhs;
this.error = error;
this.icc = icc;
}
public void print() {
System.out.println(lhs + " = " + rhs);
}
}