Java

Features ›› Wealth ›› getStockOrders ›› Sample Code ››
Parent Previous Next

Get Stock Orders


We will first import the "java.io", "java.util", "java.net" and "org.json" packages.



Next, we create a public class "GetStockOrders".

Inside the public class, we will create a public static void, and declare all the input and output variables.



Then we create a try clause, and create the header object.




Then, we will connect to the API serviceName, build the request parameters, send the request, and get the response object.




Then we will parse the Content, ServiceResponse, and ServiceRespHeader, and store the "GlobalErrorID", "ErrorText" and "ErrorDetails" in "globalErrorID", "errorText" and "errorDetails" respectively.




If the globalErrorID is '010041', it means that the OTP provided has expired. We will then display the errorText and errorDetails as well as a message 'OTP has expired, new OTP will be sent'.

If it is not '010000', we will display the ErrorText and the ErrorDetails.

Else, it means that there is no error, and we will use the "instanceof" operator to check whether the "stockorders" object is an JSONArray or JSONObject, and display the results accordingly.




Entire Code:

// GetStockOrders.java


import java.io.*;

import java.util.*;

import java.net.*;

import org.json.*;


public class GetStockOrders {


       public static void main(String args[])

       {

               

               boolean verbose = true;

       

               // api url

               String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";

               

               //response parameters

               String serviceRespTag = "Stock_Orders_ReadResponse";

               String globalErrorID;

               String errorText;

               String errorDetails;

               String serviceName = "getStockOrders";

               String userID = "KelvanTan";

               String PIN = "000000";

               String OTP = "000000";

               String buyorsell;

               String customerID;

               String expirationType;

               String intialSpotPrice;

               String maturityDate;

               String orderID;

               String orderType;

               String orderDate;

               String orderStatus;

               String priceAtExecution;

               String priceAtOrder;

               String quality;

               String settlementAccountID;

               String stockSymbol;


               try {                


                       // build header

                       JSONObject jo = new JSONObject();

                       jo.put("serviceName", serviceName);

                       jo.put("userID", userID);

                       jo.put("PIN", PIN);

                       jo.put("OTP", OTP);

                       JSONObject headerObj = new JSONObject();

                       headerObj.put("Header", jo);

                       String header = headerObj.toString();

                       

                       // connect to API service

                       HttpURLConnection urlConnection = (HttpURLConnection) new URL(apiServiceUrl).openConnection();

                       urlConnection.setDoOutput(true);

                       urlConnection.setRequestMethod("POST");

                       

                       // build request parameters

                       String parameters

                               = "Header="+header;

                               

                       // send request

                       BufferedWriter br = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()));

                       br.write(parameters);

                       br.close();

                       

                       // get response

                       String response = "";

                       Scanner s = new Scanner(urlConnection.getInputStream());

                       while (s.hasNextLine()){

                               response += s.nextLine();

                       }

                       s.close();

                       

                       // get response object

           boolean apiError;

                       JSONObject responseObj = new JSONObject();

           responseObj = new JSONObject(response);

           if (verbose)

               {

                                       //System.out.println(parameters);

                   System.out.println(responseObj.toString(4));

                   

                    System.out.println();

               }

                       //parse {"Content"}

                       JSONObject contentObj = new JSONObject();

                       contentObj = responseObj.getJSONObject("Content");

                       

                       //parse {"ServiceResponse"}

                       JSONObject serviceRespObj = contentObj.getJSONObject("ServiceResponse");

                       

                       //parse {"ServiceRespHeader"}

                       JSONObject serviceRespHeaderObj = serviceRespObj.getJSONObject("ServiceRespHeader");

                       globalErrorID = serviceRespHeaderObj.getString("GlobalErrorID");

                       errorText = serviceRespHeaderObj.getString("ErrorText");

                       errorDetails = serviceRespHeaderObj.getString("ErrorDetails");

                       

                       if(globalErrorID.equals("010041"))

                       {

                               System.out.println(errorText);

                               System.out.println(errorDetails);

                               System.out.println("OTP expired, new OTP will be sent");

                       }

                       

                       else if(!globalErrorID.equals("010000"))

                       {

                               System.out.println(errorText);

                               System.out.println(errorDetails);

                       }                

                       else

                       {

                               JSONObject stockordersObj = serviceRespObj.getJSONObject("StockOrderList");

                               Object testObj = stockordersObj.get("StockOrder");

                               if (testObj instanceof JSONArray)

                               {

                                       JSONArray soObj = stockordersObj.getJSONArray("StockOrder");

                                       for (int i = 0; i < soObj.length(); i++)

                                       {

                                               JSONObject soobj = soObj.getJSONObject(i);

                                               buyorsell = soobj.getString("buy_or_sell");

                                               customerID = soobj.getString("customerID");

                                               expirationType = soobj.getString("expiration_type");

                                               intialSpotPrice = soobj.getString("initial_spot_price");

                                               maturityDate = soobj.getString("maturity_date");

                                               orderID = soobj.getString("orderID");

                                               orderType = soobj.getString("orderType");

                                               orderDate = soobj.getString("order_date");

                                               orderStatus = soobj.getString("order_status");

                                               priceAtExecution = soobj.getString("price_at_execution");

                                               priceAtOrder = soobj.getString("price_at_order");

                                               quality = soobj.getString("quantity");

                                               settlementAccountID = soobj.getString("settlementAccountID");

                                               stockSymbol = soobj.getString("stockSymbol");

                                               

                                               System.out.println("Buy Or Sell = " +buyorsell);

                                               System.out.println("Customer ID = " +customerID);

                                               System.out.println("Expiration Type = " +expirationType);

                                               System.out.println("Intial Spot Price = " +intialSpotPrice);

                                               System.out.println("Maturity Date  = " +maturityDate);

                                               System.out.println("Order ID = " +orderID);

                                               System.out.println("Order Type = " +orderType);

                                               System.out.println("Order Date = " +orderDate);

                                               System.out.println("orderStatus = " +orderStatus);

                                               System.out.println("Price At Execution  = " +priceAtExecution);

                                               System.out.println("Price At Order = " +priceAtOrder);

                                               System.out.println("Quality = " +quality);

                                               System.out.println("Settlement Account ID = " +settlementAccountID);

                                               System.out.println("StockSymbol = " +stockSymbol);

                                       }

                               }

                               else if(testObj instanceof JSONObject)

                               {

                                       //parse product

                                       JSONObject soObj = stockordersObj.getJSONObject("StockOrder");

                                               buyorsell = soObj.getString("buy_or_sell");

                                               customerID = soObj.getString("customerID");

                                               expirationType = soObj.getString("expiration_type");

                                               intialSpotPrice = soObj.getString("initial_spot_price");

                                               maturityDate = soObj.getString("maturity_date");

                                               orderID = soObj.getString("orderID");

                                               orderType = soObj.getString("orderType");

                                               orderDate = soObj.getString("order_date");

                                               orderStatus = soObj.getString("order_status");

                                               priceAtExecution = soObj.getString("price_at_execution");

                                               priceAtOrder = soObj.getString("price_at_order");

                                               quality = soObj.getString("quantity");

                                               settlementAccountID = soObj.getString("settlementAccountID");

                                               stockSymbol = soObj.getString("stockSymbol");

                                               

                                               

                                               System.out.println("Buy Or Sell = " +buyorsell);

                                               System.out.println("Customer ID = " +customerID);

                                               System.out.println("Expiration Type = " +expirationType);

                                               System.out.println("Intial Spot Price = " +intialSpotPrice);

                                               System.out.println("Maturity Date  = " +maturityDate);

                                               System.out.println("Order ID = " +orderID);

                                               System.out.println("Order Type = " +orderType);

                                               System.out.println("Order Date = " +orderDate);

                                               System.out.println("orderStatus = " +orderStatus);

                                               System.out.println("Price At Execution  = " +priceAtExecution);

                                               System.out.println("Price At Order = " +priceAtOrder);

                                               System.out.println("Quality = " +quality);

                                               System.out.println("Settlement Account ID = " +settlementAccountID);

                                               System.out.println("Stock Symbol = " +stockSymbol);

                               }

                               else

                               {

                                       System.out.println("Customer has no Stock Orders.");

                               }

                               return;

                       }


               }

               catch(Exception e)

               {

                       e.printStackTrace(System.out);

               }

       }

}



Download

Created with the Personal Edition of HelpNDoc: Free help authoring tool