Get Stock Symbols
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetStockSymbols".
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 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.
We will then create a for-loop to loop through "ssObj".
While looping through "ssObj", we will retrieve the "symbol" of the "i" item in "ssObj", and check if it is the same as the "sym" passed in.
If it is the same, we will return company and symbol.
Entire Code:
// GetStockSymbols.java
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class GetStockSymbols {
public static void main(String sym)
{
boolean verbose = true;
// api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
//response parameters
String serviceRespTag = "Stock_Symbols_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String company;
String symbol;
String serviceName = "getStockSymbols";
String userID = "";
String PIN = "";
String OTP = "";
try {
// build header
JSONObject jo = new JSONObject();
jo.put("serviceName", serviceName);
jo.put("userID", userID); // not used
jo.put("PIN", PIN); // not used
jo.put("OTP", OTP); // not used
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 {"StockSymbolList"}
JSONObject stocksymbolsObj = serviceRespObj.getJSONObject("StockSymbolList");
//parse {"StockSymbol"}
JSONArray ssObj = stocksymbolsObj.getJSONArray("StockSymbol");
for (int i = 0; i < ssObj.length(); i++)
{
JSONObject ssobj = ssObj.getJSONObject(i);
symbol = ssobj.getString("symbol");
if(symbol.equals(sym))
{
company = ssobj.getString("company");
System.out.println("Company = " +company);
System.out.println("Symbol = " +symbol);
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Create iPhone web-based documentation