Get Affinity List
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetAffinityList".
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 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.
We will then create a for-loop to loop through "affinityObj".
While looping through "affinityObj", we will retrieve the "affID" of the "i" item in "affinityObj", and check if it is the same as the "affID" passed in.
If it is the same, we will return the "Level 1" and "Level 2".
Entire Code:
// GetAffinityList.java
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class GetAffinityList {
public static void main(String affID)
{
boolean verbose = true;
// api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
//response parameters
String serviceRespTag = "Affinity_List_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String affinityID;
String level2;
String level1;
String serviceName = "getAffinityList";
String userID = "";
String PIN = "";
String OTP = "";
try {
// build header
JSONObject jo = new JSONObject();
jo.put("serviceName","getAffinityList");
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();
// 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();
responseObj = new JSONObject(response);
if (verbose)
{
System.out.println(responseObj.toString(4));
System.out.println();
}
//parse {"Content"}
JSONObject contentObj = new JSONObject();
contentObj = responseObj.getJSONObject("Content");
//parse {"ServiceResponse"}
JSONObject serviceRespObj = contentObj.getJSONObject("ServiceResponse");
//parse {"AffinityList"}
JSONObject affinityObj = serviceRespObj.getJSONObject("AffinityList");
//parse {"Affinity"}
JSONArray affObj = affinityObj.getJSONArray("Affinity");
for (int i = 0; i < affObj.length(); i++)
{
JSONObject affobj = affObj.getJSONObject(i);
affinityID = affobj.getString("AffinityID");
if(affinityID.equals(affinityID))
{
level1 = affobj.getString("Level1");
level2 = affobj.getString("Level2");
System.out.println("Affinity ID = " +affinityID);
System.out.println("Level 2 = " +level2);
System.out.println("Level 1 = " +level1);
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Free EPub producer