Get Billing Organizations
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "getBillingOrganizations".
Inside the public class, we will create a public void "getBillingOrganizations", which requires "billingOrganizationID" 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 "billingOrgArray".
While looping through "billingOrgArray", we will retrieve the "BeneficiaryID" of the "i" item in "billingOrgArray", and check if it is the same as the "customerID" passed in.
If it is the same, we will return the "BillingOrgName".
Entire Code:
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class getBillingOrganizations
{
public void getBillingOrganizations(String billingOrganizationID)
{
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
try {
// build header
JSONObject jo = new JSONObject();
String serviceName = "getBillingOrganizations";
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 {"BillingOrgList"}
JSONObject billingOrgListObj = serviceRespObj.getJSONObject("BillingOrgList");
// parse {"BillingOrg"}
JSONArray billingOrgArray = billingOrgListObj.getJSONArray("BillingOrg");
for(int i = 0; i < billingOrgArray.length(); i++)
{
JSONObject billingOrg = billingOrgArray.getJSONObject(i);
String benefID = billingOrg.getString("BeneficiaryID");
if(billingOrganizationID.equals(benefID))
{
//return BillingOrgName;
String billingOrgName = billingOrg.getString("BillingOrgName");
//return BillingOrgName;
System.out.println("Billing Organization Name = "+ billingOrgName);
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Easily create Qt Help files