Login Staff
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "LoginStaff".
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 both the header and content 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 display the result.
Entire Code:
// LoginStaff.java
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class LoginStaff {
public static void main(String args[]){
// api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
boolean verbose = true;
// response parameters
String globalErrorID;
String errorText;
String errorDetails;
String BIC;
String BankAddress;
String BankBranch;
String BankID;
String BankLogoLocation;
String BankName;
String BaseCurrency;
String Firstname;
String LastLoginTimestamp;
String Lastname;
String Usertype;
String username = "Alan"
String = "123456"
application = "Teller"
try {
// build header
JSONObject jo = new JSONObject();
jo.put("serviceName", "loginStaff");
jo.put("userID", ""); // not used
jo.put("PIN", ""); // not used
jo.put("OTP", ""); // not used
JSONObject headerObj = new JSONObject();
headerObj.put("Header", jo);
String header = headerObj.toString();
// build content
jo = new JSONObject();
jo.put("username", username);
jo.put("password", password);
jo.put("application", application);
JSONObject contentObj = new JSONObject();
contentObj.put("Content", jo);
String content = contentObj.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+"&"
+ "Content="+content+"&"
+ "ConsumerID=Teller"; // this API is for Teller only
// 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(response);
responseObj = new JSONObject(response);
if (verbose)
{
System.out.println(responseObj.toString(4)); // indent 4 spaces
System.out.println();
}
// parse {"Content"}
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 (verbose){
System.out.println("GlobalErrorID="+globalErrorID);
System.out.println("ErrorText="+errorText);
System.out.println("ErrorDetails="+errorDetails);
System.out.println();
}
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 StaffLoginAuthenticateResponseObj = serviceRespObj.getJSONObject("Staff_Login_Authenticate-Response");
BIC = StaffLoginAuthenticateResponseObj.getString("BIC");
BankAddress = StaffLoginAuthenticateResponseObj.getString("BankAddress");
BankBranch = StaffLoginAuthenticateResponseObj.getString("BankBranch");
BankID = StaffLoginAuthenticateResponseObj.getString("BankID");
BankLogoLocation = StaffLoginAuthenticateResponseObj.getString("BankLogoLocation");
BankName = StaffLoginAuthenticateResponseObj.getString("BankName");
BaseCurrency = StaffLoginAuthenticateResponseObj.getString("BaseCurrency");
Firstname = StaffLoginAuthenticateResponseObj.getString("Firstname");
LastLoginTimestamp = StaffLoginAuthenticateResponseObj.getString("LastLoginTimestamp");
Lastname = StaffLoginAuthenticateResponseObj.getString("Lastname");
Usertype = StaffLoginAuthenticateResponseObj.getString("Usertype");
System.out.println("BIC="+BIC);
System.out.println("BankAddress="+BankAddress);
System.out.println("BankBranch="+BankBranch);
System.out.println("BankID="+BankID);
System.out.println("BankLogoLocation="+BankLogoLocation);
System.out.println("BankName="+BankName);
System.out.println("BaseCurrency="+BaseCurrency);
System.out.println("Firstname="+Firstname);
System.out.println("LastLoginTimestamp="+LastLoginTimestamp);
System.out.println("Lastname="+Lastname);
System.out.println("Usertype="+Usertype);
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Create cross-platform Qt Help files