Get Transaction Types
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "getTransactionTypes".
Inside the public class, we will create a public void "getTransactionTypes", which requires "transactionTypeID" 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 "transactionTypeArray".
While looping through "transactionTypeArray", we will retrieve the "TransactionTypeID" of the "i" item in "transactionTypeArray", and check if it is the same as the "customerID" passed in.
If it is the same, we will return the "TransactionTypeName".
Entire Code:
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class getTransactionTypes
{
public void getTransactionTypes(String transactionTypeID)
{
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
String custName;
try {
// build header
JSONObject jo = new JSONObject();
String serviceName = "getTransactionTypes";
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 {"TransactionTypeList"}
JSONObject transactionTypeListObj = serviceRespObj.getJSONObject("TransactionTypeList");
// parse {"TransactionType"}
JSONArray transactionTypeArray = transactionTypeListObj.getJSONArray("TransactionType");
for(int i = 0; i < transactionTypeArray.length(); i++)
{
JSONObject transactionType = transactionTypeArray.getJSONObject(i);
String transactionTypeID = transactionType.getString("TransactionTypeID");
if(transactionTypeID.equals(transactionID))
{
//return TransactionTypeName;
String transactionName = transactionType.getString("TransactionTypeName");
//display TransactionTypeName;
System.out.println("Transaction Type Name = "+ transactionName);
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Produce online help for Qt applications