In this post, I will show you how to retrieve currency exchange rate from yahoo finance api by Java program.
There are some apis for getting exchange rate - reuters, bloomberg, yahoo finance etc.
If you don't have to get high-ratency realtime exchange rate, the most simple and free api - yahoo finance api - is enough.
Okay, here is the actual code. The api returns exchange rate as a Rate object.
The below interface is a generic interface for getting exchange rate.
This is rate class.
There are some apis for getting exchange rate - reuters, bloomberg, yahoo finance etc.
If you don't have to get high-ratency realtime exchange rate, the most simple and free api - yahoo finance api - is enough.
Okay, here is the actual code. The api returns exchange rate as a Rate object.
package com.dukesoftware.exchangerate.api;
import static java.lang.String.format;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
public class YahooExchangeRateApi implements ExchangeRateApi {
// url for yahoo finance api
private final static String URL_FORMAT = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=%s%s=X";
public Rate getRate(String ccy1, String ccy2)
{
assertNotNull(ccy1, "Invalid ccy1" + ccy1);
assertNotNull(ccy2, "Invalid ccy2" + ccy2);
String urlStr = format(URL_FORMAT, ccy1, ccy2);
HttpURLConnection connection = null;
InputStream is = null;
try
{
URI uri = new URI(urlStr);
connection = (HttpURLConnection)uri.toURL().openConnection();
connection.connect();
is = connection.getInputStream();
// raw return string is formatted as csv
String content = toString(is);
String[] data = content.split(",");
Rate rate = new Rate();
rate.setCcy1(ccy1);
rate.setCcy2(ccy2);
rate.setDate(data[2].replaceAll("\"", ""));
rate.setValue(Double.parseDouble(data[1]));
return rate;
}
catch(Exception e)
{
throw new IllegalStateException("fail to get rate for " + ccy1 + ccy2, e);
}
finally{
closeQuietly(is);
if(connection != null)
{
connection.disconnect();
}
}
}
// utility methods
private static String toString(InputStream is) throws IOException
{
byte[] buf = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for(int len = is.read(buf); len > 0; len = is.read(buf))
{
baos.write(buf, 0, len);
}
return baos.toString();
}
private static void assertNotNull(Object object, String message)
{
if(object == null)
{
throw new IllegalArgumentException(message);
}
}
private static void closeQuietly(Closeable closeable)
{
if(closeable != null)
{
try {
closeable.close();
} catch (IOException e) {
}
}
}
}
The below interface is a generic interface for getting exchange rate.
package com.dukesoftware.exchangerate.api;
public interface ExchangeRateApi {
Rate getRate(String ccy1, String ccy2);
}
This is rate class.
package com.dukesoftware.exchangerate.api;
public class Rate {
private String ccy1;
private String ccy2;
private Double value;
private String date;
public String getCcy1() {
return ccy1;
}
public void setCcy1(String ccy1) {
this.ccy1 = ccy1;
}
public String getCcy2() {
return ccy2;
}
public void setCcy2(String ccy2) {
this.ccy2 = ccy2;
}
public Double getValue() {
return value;
}
public void setValue(Double value) {
this.value = value;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "ccy1:"+ccy1+",ccy2:"+ccy2+",date:" + date + ",value:" + value;
}
}
コメント