Get Customer Appointments
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetCustomerAppointments".
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 "account" object is an JSONArray or JSONObject, and display the results accordingly.
Entire Code:
// GetCustomerAppointments.java
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class GetCustomerAppointments {
public static void main(String args[]){
// api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
boolean verbose = true;
String globalErrorID;
String errorText;
String errorDetails;
String acceptedBy;
String appointmentID;
String bankID;
String branchID;
String customerID;
String date;
String narrative;
String purpose;
String relatedAccountID;
String timeslot;
String serviceName = "getCustomerAppointments";
String PIN = "123456";
String userID = "alan1960";
String OTP = "123456";
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(response);
responseObj = new JSONObject(response);
if (verbose)
{
System.out.println(responseObj.toString(4)); // indent 4 spaces
System.out.println();
}
JSONObject contentObj = new JSONObject();
//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(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 ClientAppointmentList = serviceRespObj.getJSONObject("ClientAppointmentList");
Object testObj = ClientAppointmentList.get("ClientAppointment");
if (testObj instanceof JSONArray)
{
JSONArray ClientAppointmentArray = ClientAppointmentList.getJSONArray("ClientAppointment");
for(int i = 0; i < ClientAppointmentArray.length(); i++)
{
JSONObject CAA = ClientAppointmentArray.getJSONObject(i);
acceptedBy = CAA.getString("acceptedBy");
appointmentID = CAA.getString("appointmentID");
bankID = CAA.getString("bankID");
branchID = CAA.getString("branchID");
customerID = CAA.getString("customerID");
date = CAA.getString("date");
narrative = CAA.getString("narrative");
purpose = CAA.getString("purpose");
relatedAccountID = CAA.getString("relatedAccountID");
timeslot = CAA.getString("timeslot");
System.out.println("acceptedBy="+acceptedBy);
System.out.println("appointmentID="+appointmentID);
System.out.println("bankID="+bankID);
System.out.println("branchID="+branchID);
System.out.println("customerID="+customerID);
System.out.println("date="+date);
System.out.println("narrative="+narrative);
System.out.println("purpose="+purpose);
System.out.println("relatedAccountID="+relatedAccountID);
System.out.println("timeslot="+timeslot);
}
}
else if(testObj instanceof JSONObject)
{
JSONObject ClientAppointmentObj = ClientAppointmentList.getJSONObject("ClientAppointment");
acceptedBy = ClientAppointmentObj.getString("acceptedBy");
appointmentID = ClientAppointmentObj.getString("appointmentID");
bankID = ClientAppointmentObj.getString("bankID");
branchID = ClientAppointmentObj.getString("branchID");
customerID = ClientAppointmentObj.getString("customerID");
date = ClientAppointmentObj.getString("date");
narrative = ClientAppointmentObj.getString("narrative");
purpose = ClientAppointmentObj.getString("purpose");
relatedAccountID = ClientAppointmentObj.getString("relatedAccountID");
timeslot = ClientAppointmentObj.getString("timeslot");
System.out.println("acceptedBy="+acceptedBy);
System.out.println("appointmentID="+appointmentID);
System.out.println("bankID="+bankID);
System.out.println("branchID="+branchID);
System.out.println("customerID="+customerID);
System.out.println("date="+date);
System.out.println("narrative="+narrative);
System.out.println("purpose="+purpose);
System.out.println("relatedAccountID="+relatedAccountID);
System.out.println("timeslot="+timeslot);
}
else
{
System.out.println("No Appointment.");
}
return;
}
}
catch(Exception e) {e.printStackTrace(System.out);}
}
}
Created with the Personal Edition of HelpNDoc: What is a Help Authoring tool?