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...

Step 1. Install Eclipse

  1. Install Java JDK 7 and Eclipse on your system.
  2. 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.

  1. Create a new clean Java Project by going to File > New > Java ProjectScreen1
  2. Leave all settings default, set only the desired project name.Screen2

Step 3. Add xAPI wrapper to the project

  1. Select your project with right click and choose > Import...Screen3
  2. Choose General > File system in the opened dialog window.Screen4
  3. Now click Browse… and provide the path where you have your xAPI wrapper downloaded and unpacked in the next window.Screen5
  4. Check the Overwrite existing resources without warning checkbox and click the Finish buttonScreen6

Now, wrapper are ready so we can create our first project!

Step 4. Create new project

  1. Create new project by clicking File > New > Java Project.Screen9
  2. Enter a name for your project and click Finish button.Screen10

Step 5. Add xAPI Wrapper to build path

  1. Click right mouse button on your project and select Properties.Screen11
  2. Go to the Java Build Path > Projects section and click Add... button.Screen12
  3. In the opened window choose xApi project, which contains xAPI wrappers.Screen13

Now you are ready to write some code!

Step 6. Add a new class

  1. Click on your project with right mouse button and selecting New > Class.Screen14
  2. Enter a name for your class (for example: Test) and check the public static void main(String[] args) field.Screen12

Step 7. Run example code

  1. Replace all the code in the class created in previous step with the code given below:
  2. 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();
    		}
    	}
    }							
    
    
  3. Insert your login credentials to the demo account to the executeLoginCommand function.
  4. Click F11 to run the program.
  5. The list of all symbols with their prices will be displayed in the console.

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!