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));

20 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
  12. Just pure classic stuff from you here. I have never seen such a brilliantly written article in a long time. I am thankful to you that you produced this! Netflix Stock updates

    ReplyDelete
  13. I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me. convert money calculator

    ReplyDelete
  14. There are numerous sorts of cash converter accessible and these will regularly cover you for an assortment of conditions. convert money

    ReplyDelete
  15. Thank you for taking the time to publish this information very useful! convert money online

    ReplyDelete
  16. In addition, you likewise don't need to search for the closest cash changer to make sure you can perceive how much a Rupee costs in U.S. dollars. convert money

    ReplyDelete
  17. Hi there! Nice stuff, do keep me posted when you post again something like this! convert currency

    ReplyDelete
  18. With these free money making thoughts the way that your hazard diminishes for quite a while insofar as you're still in the exchange is the most significant piece of this technique.convert money online

    ReplyDelete
  19. Howdy, I discovered your blog through Google while hunting down such kinda instructive post and your post looks extremely intriguing for me.
    binance

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts