October 22, 2017

Real Time Currency Converter in java - Webservice

Currency Converter API (xe.com)


Description


some time instead of keeping currency rate converter static data in your local database is not at all feasible for your project. Yahoo is providing a free web sevices for currency rate converter similar to google xe.com. The below code will return the currency converting ratio

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 *
 */

/**
 * @author currency conversion
 *
 */
public class DCSCurrencyConvertor {



/**
* @param args
* @throws IOException
* @throws HttpException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub;

DCSCurrencyConvertor convertor = new DCSCurrencyConvertor();
try {
String rate = convertor.convert("USD","AED");
System.out.println("USD to AED :"+rate);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}





}

private String convert(String currencyFrom, String currencyTo) throws HttpException, IOException {
String currentRate = null;
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod("http://quote.yahoo.com/d/quotes.csv?s=" + currencyFrom + currencyTo + "=X&f=l1&e=.csv");
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    new DefaultHttpMethodRetryHandler(3, false));
int status = client.executeMethod(method);
System.out.println("status : "+status);

byte[] response = method.getResponseBody();
currentRate =new String(response);

return currentRate;
}

}

Jar files (Application Programming Interface)

commons-httpclient-3.1.jar
commons-codec-1.11.jar


Uri (Uniform Resource locator )


http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDEUR%22,%20%22USDJPY%22,%20%22USDBGN%22,%20%22USDCZK%22,%20%22USDDKK%22,%20%22USDGBP%22,%20%22USDHUF%22,%20%22USDLTL%22,%20%22USDLVL%22,%20%22USDPLN%22,%20%22USDRON%22,%20%22USDSEK%22,%20%22USDCHF%22,%20%22USDNOK%22,%20%22USDHRK%22,%20%22USDRUB%22,%20%22USDTRY%22,%20%22USDAUD%22,%20%22USDBRL%22,%20%22USDCAD%22,%20%22USDCNY%22,%20%22USDHKD%22,%20%22USDIDR%22,%20%22USDILS%22,%20%22USDINR%22,%20%22USDKRW%22,%20%22USDMXN%22,%20%22USDMYR%22,%20%22USDNZD%22,%20%22USDPHP%22,%20%22USDSGD%22,%20%22USDTHB%22,%20%22USDZAR%22,%20%22USDISK%22)&env=store://datatables.org/alltableswithkeys


Thanks to steffi for her support

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments