FIRST STEPS WITH XAPI, JAVA AND ECLIPSE
In this tutorial, you will learn how to start to use Java wrappers. What is more, you will be able to create your first application using xAPI. The tutorial does not require any knowledge of Eclipse IDE.
Commands used in this tutorial:
- login,
- getAllSymbols
Before you start...
- To complete this tutorial, you need Java JDK 7 ,that can be downloaded from the Oracle website: oracle.com/technetwork/java/javase/downloads.
- Eclipse IDE (Juno) for Java Developers, that could be downloaded from: eclipse.org/downloads.
- xAPI Java Wrapper, that could be downloaded from http://developers.xstore.pro/api/wrappers.
- To connect to xAPI you need a test trading demo account which you received after the registration.
Step 1. Install Eclipse
- Install Java JDK 7 and Eclipse on your system.
- Make sure, that you have your JRE listed in Window > Preferences > Java > Installed JREs. If not, please click on Add… and provide a path where you had your Java installed.
Step 2. Create new project
Now, as you have your IDE configured, you can start working with our API.
Step 3. Add xAPI wrapper to the project
- Select your project with right click and choose > Import...
- Choose General > File system in the opened dialog window.
- Now click Browse… and provide the path where you have your xAPI wrapper downloaded and unpacked in the next window.
- Check the Overwrite existing resources without warning checkbox and click the Finish button
Now, wrapper are ready so we can create our first project!
Step 4. Create new project
Step 5. Add xAPI Wrapper to build path
Now you are ready to write some code!
Step 6. Add a new class
Step 7. Run example code
- Replace all the code in the class created in previous step with the code given below:
- Insert your login credentials to the demo account to the executeLoginCommand function.
- Click F11 to run the program.
- The list of all symbols with their prices will be displayed in the console.
import java.io.IOException; import java.net.UnknownHostException; import pro.xstore.api.message.command.APICommandFactory; import pro.xstore.api.message.error.APICommandConstructionException; import pro.xstore.api.message.error.APICommunicationException; import pro.xstore.api.message.error.APIReplyParseException; import pro.xstore.api.message.records.SymbolRecord; import pro.xstore.api.message.response.APIErrorResponse; import pro.xstore.api.message.response.AllSymbolsResponse; import pro.xstore.api.message.response.LoginResponse; import pro.xstore.api.sync.Credentials; import pro.xstore.api.sync.ServerData.ServerEnum; import pro.xstore.api.sync.SyncAPIConnector; public class Test { public static void main(String[] args) { try { // Create new connector SyncAPIConnector connector = new SyncAPIConnector(ServerEnum.DEMO); // Create new credentials // TODO: Insert your credentials Credentials credentials = new Credentials(10000L, "password"); // Create and execute new login command LoginResponse loginResponse = APICommandFactory.executeLoginCommand( connector, // APIConnector credentials // Credentials ); // Check if user logged in correctly if(loginResponse.getStatus() == true) { // Print the message on console System.out.println("User logged in"); // Create and execute all symbols command (which gets list of all symbols available for the user) AllSymbolsResponse availableSymbols = APICommandFactory.executeAllSymbolsCommand(connector); // Print the message on console System.out.println("Available symbols:"); // List all available symbols on console for(SymbolRecord symbol : availableSymbols.getSymbolRecords()) { System.out.println("-> " + symbol.getSymbol() + " Ask: " + symbol.getAsk() + " Bid: " + symbol.getBid()); } } else { // Print the error on console System.err.println("Error: user couldn't log in!"); } // Close connection connector.close(); System.out.println("Connection closed"); // Catch errors } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (APICommandConstructionException e) { e.printStackTrace(); } catch (APICommunicationException e) { e.printStackTrace(); } catch (APIReplyParseException e) { e.printStackTrace(); } catch (APIErrorResponse e) { e.printStackTrace(); } } }
Summary
In this tutorial you have learned how to create and run your first xAPI Java project in Eclipse. Now you are ready to create your own application!