Get Currency List
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetCurrencyList".
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 "curObj".
While looping through "curObj", we will retrieve the "CurrencyCode" of the "i" item in "curObj", and check if it is the same as the "ccode" passed in.
If it is the same, we will return countryName, currencyName and currencyCode.
Entire Code:
// GetcurrencyList.java
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class GetCurrencyList {
public static void main(String ccode)
{
boolean verbose = true;
// api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
//response parameters
String serviceRespTag = "Currency_List_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String countryName;
String currencyCode;
String currencyName;
String serviceName = "getCurrencyList";
String userID = "";
String PIN = "";
String OTP = "";
try {
// build header
JSONObject jo = new JSONObject();
jo.put("serviceName", serviceName);
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(parameters);
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 {"CurrencyList"}
JSONObject currencyObj = serviceRespObj.getJSONObject("CurrencyList");
//parse {"Currency"}
JSONArray curObj = currencyObj.getJSONArray("Currency");
for (int i = 0; i < curObj.length(); i++)
{
JSONObject curobj = curObj.getJSONObject(i);
currencyCode = curobj.getString("CurrencyCode");
if(currencyCode.equals(ccode))
{
countryName = curobj.getString("CountryName");
currencyName = curobj.getString("CurrencyName");
System.out.println("Country Name = " +countryName);
System.out.println("Currency Code = " +currencyCode);
System.out.println("Currency Name = " +currencyName);
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Easily create iPhone documentation