Post
Topic
Board Project Development
Re: [Announce] XChange - A Financial Exchange Library for Java V1.3.0
by
timmolter
on 17/01/2013, 10:53:50 UTC
Good stuff - that caching simplifies what I have to do as I then don't have to worry if I am hitting the server too often.

Here's some example code from the xchange-examples jar to demonstrate using the api key and requesting the data from OpenExchangeRates:
Code:
   // Use the factory to get the Open Exchange Rates exchange API
    ExchangeSpecification exchangeSpecification = new ExchangeSpecification("com.xeiam.xchange.oer.OERExchange");
    exchangeSpecification.setUri("http://openexchangerates.org");
    exchangeSpecification.setApiKey("ea88c922bca263ba9345b4717914ee1f");
    Exchange openExchangeRates = ExchangeFactory.INSTANCE.createExchange(exchangeSpecification);

    // Interested in the polling market data feed
    PollingMarketDataService marketDataService = openExchangeRates.getPollingMarketDataService();

    // Get the latest ticker data showing EUR/USD
    Ticker ticker = marketDataService.getTicker(Currencies.EUR, Currencies.USD);
    System.out.println("Last: " + ticker.getLast().toString());

    // Request another ticker JPY/USD. it will return a cached object
    ticker = marketDataService.getTicker(Currencies.JPY, Currencies.USD);
    System.out.println("cached Last: " + ticker.getLast().toString());

...and the result...

Code:
Last: EUR 0.752646
cached Last: JPY 88.740257