Get Game Leaders
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetGameLeaders".
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 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.
If globalErrorID is not '010000', we will display the ErrorText and the ErrorDetails.
Else, it means that there is no error, and we will use the "instanceof" operator to check whether the "Leaders" object is an JSONArray or JSONObject, and display the results accordingly.
Entire Code:
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class GetGameLeaders
{
public static void main(String args[])
{
boolean verbose = true;
//api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
String serviceRespTag = "Get_Game_Leaders_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String score;
String user_Id;
String serviceName = "getGameLeaders";
String userID = "";
String PIN = "";
String OTP = "";
String gameID = "1";
String start = "2010-01-01 00:00:00";
String end = "2019-01-01 23:59:59";
String mode = "*";
String byGroup = "true";
try
{
//building of header
JSONObject jo = new JSONObject();
jo.put("serviceName", serviceName);
jo.put("userID", userID);
jo.put("PIN", PIN);
jo.put("OTP", OTP);
JSONObject headerObj = new JSONObject();
headerObj.put("Header", jo);
String header = headerObj.toString();
//building of content
jo = new JSONObject();
jo.put("gameID", gameID);
jo.put("start", start);
jo.put("end", end);
jo.put("mode", mode);
jo.put("byGroup", byGroup);
JSONObject contentObj = new JSONObject();
contentObj.put("Content", jo);
String content = contentObj.toString();
//Connect to the API Service
HttpURLConnection urlConnection = (HttpURLConnection) new URL(apiServiceUrl).openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
//build request parameters
String parameters = "Header=" +header+ "&" + "Content=" +content;
//sending of the request
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()));
br.write(parameters);
br.close();
//getting the response
String response = "";
Scanner s = new Scanner(urlConnection.getInputStream());
while (s.hasNextLine())
{
response += s.nextLine();
}
s.close();
//getting the response object
boolean apiError;
JSONObject responseObj = new JSONObject(response);
responseObj = new JSONObject(response);
if (verbose)
{
System.out.println(responseObj.toString(4));
System.out.println();
}
//parsing of content
contentObj = responseObj.getJSONObject("Content");
//parsing of the service Response
JSONObject serviceRespObj = contentObj.getJSONObject("ServiceResponse");
// parsing the service response header
JSONObject serviceRespHeaderObj = serviceRespObj.getJSONObject("ServiceRespHeader");
globalErrorID = serviceRespHeaderObj.getString("GlobalErrorID");
errorText = serviceRespHeaderObj.getString("ErrorText");
errorDetails = serviceRespHeaderObj.getString("ErrorDetails");
if(!globalErrorID.equals("010000"))
{
System.out.println(errorText);
System.out.println(errorDetails);
}
else
{
JSONObject leaderObj = serviceRespObj.getJSONObject("LeaderDetails");
JSONObject leaderObj2 = leaderObj.getJSONObject("Leaders");
Object testObj = leaderObj2.get("Leader");
if (testObj instanceof JSONArray)
{
JSONArray ldrObj = leaderObj2.getJSONArray("Leader");
for (int i = 0; i < ldrObj.length(); i++)
{
JSONObject ldrobj = ldrObj.getJSONObject(i);
score = ldrobj.getString("score");
user_Id = ldrobj.getString("user_Id");
System.out.println("score = " + score);
System.out.println("user_id = " + user_Id);
}
}
else if(testObj instanceof JSONObject)
{
JSONObject ldrObj = leaderObj2.getJSONObject("Leader");
score = ldrObj.getString("score");
user_Id = ldrObj.getString("user_Id");
System.out.println("score = " + score);
System.out.println("user_id = " + user_Id);
}
else
{
System.out.println("No record");
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: iPhone web sites made easy