Get All Product Details
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetAllProductDetails".
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 use the "instanceof" operator to check whether the "product" object is an JSONArray or JSONObject, and display the results accordingly.
Entire Code:
// GetAllProductDetails.java
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class GetAllProductDetails
{
public static void main(String args[])
{
boolean verbose = true;
// api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
//response parameters
String serviceRespTag = "All_Product_Details_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String productName;
String minOpeningBalance;
String penaltyRate;
String productID;
String maxLtvRatio;
String repaymentPenaltyThreshold;
String term;
String interestRate;
String serviceName = "getAllProductDetails";
String userID = "";
String PIN = "";
String OTP = "";
String bankID = "1";
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();
// build content
jo = new JSONObject();
jo.put("bankID", bankID);
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;
// 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"}
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 productObj2 = serviceRespObj.getJSONObject("Product_ParametersList_Read-Response");
JSONObject productObj = productObj2.getJSONObject("ProductList");
Object testObj = productObj.get("Product");
if (testObj instanceof JSONArray)
{
JSONArray proObj = productObj.getJSONArray("Product");
for (int i = 0; i < proObj.length(); i++)
{
JSONObject proobj = proObj.getJSONObject(i);
productName = proobj.getString("ProductName");
minOpeningBalance = proobj.getString("MinOpeningBalance");
penaltyRate = proobj.getString("PenaltyRate");
productID = proobj.getString("ProductID");
maxLtvRatio = proobj.getString("MaxLtvRatio");
repaymentPenaltyThreshold = proobj.getString("RepaymentPenaltyThreshold");
term = proobj.getString("Term");
interestRate = proobj.getString("InterestRate");
System.out.println("Product Name = " +productName);
System.out.println("Min Opening Balance = " +minOpeningBalance);
System.out.println("PenaltyRate = " +penaltyRate);
System.out.println("ProductID = " +productID);
System.out.println("MaxLtvRatio = " +maxLtvRatio);
System.out.println("RepaymentPenaltyThreshold = " +repaymentPenaltyThreshold);
System.out.println("Term = " +term);
System.out.println("InterestRate = " +interestRate);
}
}
else if(testObj instanceof JSONObject)
{
//parse product
JSONObject proObj = productObj.getJSONObject("account");
productName = proObj.getString("ProductName");
minOpeningBalance = proObj.getString("MinOpeningBalance");
penaltyRate = proObj.getString("PenaltyRate");
productID = proObj.getString("ProductID");
maxLtvRatio = proObj.getString("MaxLtvRatio");
repaymentPenaltyThreshold = proObj.getString("RepaymentPenaltyThreshold");
term = proObj.getString("Term");
interestRate = proObj.getString("InterestRate");
System.out.println("Product Name = " +productName);
System.out.println("Min Opening Balance = " +minOpeningBalance);
System.out.println("PenaltyRate = " +penaltyRate);
System.out.println("ProductID = " +productID);
System.out.println("MaxLtvRatio = " +maxLtvRatio);
System.out.println("RepaymentPenaltyThreshold = " +repaymentPenaltyThreshold);
System.out.println("Term = " +term);
System.out.println("InterestRate = " +interestRate);
}
else
{
System.out.println("Customer has no product.");
}
return;
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Produce online help for Qt applications