Get Customer Types
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "getCustomerTypes".
Inside the public class, we will create a public void "getCustomerTypes", which requires "customerID" to be passed in.
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.
We will then create a for-loop to loop through "customerTypeArray".
While looping through "customerTypeArray", we will retrieve the "CustomerTypeID" of the "i" item in "customerTypeArray", and check if it is the same as the "customerID" passed in.
If it is the same, we will return the "CustomerTypeName".
Entire Code:
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class getCustomerTypes
{
public void getCustomerTypes(String customerID)
{
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
String custName;
try {
// build header
JSONObject jo = new JSONObject();
String serviceName = "getCustomerTypes";
jo.put("serviceName", serviceName);
jo.put("userID", "");
jo.put("PIN", "");
jo.put("OTP", "");
JSONObject headerObj = new JSONObject();
headerObj.put("Header", jo);
String header = headerObj.toString();
// build content
jo = new JSONObject();
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
JSONObject responseObj = new JSONObject(response);
// parse {"Content"}
contentObj = responseObj.getJSONObject("Content");
// parse {"ServiceResponse"}
JSONObject serviceRespObj = contentObj.getJSONObject("ServiceResponse");
// parse {"CustomerTypeList"}
JSONObject customerTypeListObj = serviceRespObj.getJSONObject("CustomerTypeList");
// parse {"CustomerType"}
JSONArray customerTypeArray = customerTypeListObj.getJSONArray("CustomerType");
for(int i = 0; i < customerTypeArray.length(); i++)
{
JSONObject customerType = customerTypeArray.getJSONObject(i);
String customerTypeID = customerType.getString("CustomerTypeID");
if(customerID.equals(customerTypeID))
{
//return customerType;
custName = customerType.getString("CustomerTypeName");
//display custTypeName;
System.out.println("Customer Type Name = "+ custName);
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Create cross-platform Qt Help files