To convert money from one currency to another currency,  we need Currency Exchange Rate Value. Yahoo is providing an online API to get currency exchange rates. Here in below example I explained how to convert Money from one currency to other using Yahoo Finance API.

Steps to Convert Currency :

Yahoo Finance API :

Observe below GET request URL. You can see the query in q parameter. Here the Currency codes were paired. Click on below link to see output XML format.

Example :

If you want to get US dollar value in Indian rupees then you have to form below URL
String url ="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&env=store://datatables.org/alltableswithkeys"; 
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);

// Send request
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode + "\n\n");
BufferedReader in = new BufferedReader(
       new InputStreamReader(con.getInputStream()));
String outputLine;

// reading output from Request
StringBuffer response = new StringBuffer(); 
while ((outputLine = in.readLine()) != null) {
   response.append(outputLine);
}
in.close();

Convert XML Response to POJO class :

// Converting XML to JSON and then JSON to POJO Classes
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
QueryPojo mp = gson.fromJson(Utility.convertXMLtoJSON(response.toString()), QueryPojo.class);

Convert Currency With Exchange Rate :

String exchangeRate = mp.getQuery().getResults().getRate().getRate();
BigDecimal amountOne = new BigDecimal(amout);
BigDecimal rate = new BigDecimal(exchangeRate);
System.out.println(amountOne.multiply(rate));

12 comments:

  1. hi! how can i get oil prise by same method?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

  3. Calculate the live currency and foreign exchange rates with free currency converter. You can convert currencies instantly. I recmonded you Currency calculator app through this you get more results about Money converter .

    ReplyDelete
  4. I am looking for and I love to post a comment that "The content of your post is awesome" Great work Up to 10 VIP signals Daily with updates.

    ReplyDelete
  5. hi was just seeing if you minded a comment. i like your website and the thme you picked is super. I will be back. exchange rates widget

    ReplyDelete
  6. There are numerous kinds of currency converters on the web. The sorts vary by their planned purposes and the data they give. exchange rates widget

    ReplyDelete
  7. The best projects return with full cash ensures on them and even empower you to demo exchange them inside the bounds of a sheltered practice record to see and guarantee that they perform a long time before you give them any genuine cash to contribute with to make the whole procedure hazard free. naked url with no anchor

    ReplyDelete
  8. Hey. Very cool website!! Man .. Excellent .. Superb .. I’ll bookmark this website and take the feeds additionally…I’m glad to find a lot of helpful info here within the article. Thank you for sharing.. usd to eur

    ReplyDelete
  9. A currency converter is an exceptionally usd to aud convenient device for Forex world as well as just as every day life. When you travel, request merchandise over web it's important to discover current estimation of remote currency

    ReplyDelete
  10. I love significantly your own post! I look at all post is great. I discovered your personal content using bing search. Discover my webpage is a great one as you.I work to create several content this post. Once more you can thank you and keep it create! Enjoy! stock market

    ReplyDelete
  11. I went over this website and 200 euros to usd
    I believe you have a lot of wonderful information, saved to my bookmarks

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts