Get Product Types
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "getProductTypes".
Inside the public class, we will create a public void "getProductTypes", which requires "productID" 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 "productArray".
While looping through "productArray", we will retrieve the "ProductID" of the "i" item in "productArray", and check if it is the same as the "productID" passed in.
If it is the same, we will return the "ProductName".
Entire Code:
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class getProductTypes
{
public void getProductTypes(String productID)
{
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
try {
// build header
JSONObject jo = new JSONObject();
String serviceName = "getProductTypes";
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 {"ProductList"}
JSONObject productListObj = serviceRespObj.getJSONObject("ProductList");
// parse {"Product"}
JSONArray productArray = productListObj.getJSONArray("Product");
for(int i = 0; i < productArray.length(); i++)
{
JSONObject productTypes = productArray.getJSONObject(i);
String prodID = productTypes.getString("ProductID");
if(productID.equals(prodID))
{
//return ProductName;
String productName = productTypes.getString("ProductName");
//display ProductName;
System.out.println("Product Type = "+ productName);
}
else
{
System.out.println("No record found.");
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Free HTML Help documentation generator