Get Game Question
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetGameQuestion".
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 display the result.
Entire Code:
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class GetGameQuestion
{
public static void main(String args[])
{
boolean verbose = true;
//api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
//request parameters
String serviceRespTag = "Get_Game_Question_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String category;
String question;
String choices;
String serviceName = "getGameQuestion";
String userID = "";
String PIN = "";
String OTP = "";
String questionID = "1";
try
{
//build 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();
//build content
jo = new JSONObject();
jo.put("questionID", questionID);
JSONObject contentObj = new JSONObject();
contentObj.put("Content", jo);
String content = contentObj.toString();
//conntect 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 reponse object
boolean apiError;
JSONObject responseObj = new JSONObject();
responseObj = new JSONObject(response);
if (verbose)
{
System.out.println(responseObj.toString(4));
System.out.println();
}
//parse {"Content"}
contentObj = responseObj.getJSONObject("Content");
//parse {"ServiceResponse"}
JSONObject serviceRespObj = contentObj.getJSONObject("ServiceResponse");
//parse {"ServiceRespHeader"}
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 qdoj = new JSONObject();
qdoj = serviceRespObj.getJSONObject("QuestionDetails");
category = qdoj.getString("category");
question = qdoj.getString("question");
choices = qdoj.getString("choices");
System.out.println("Category : "+category);
System.out.println(question);
String labels[]=new String[5];
labels[0] = "a) ";
labels[1] = "b) ";
labels[2] = "c) ";
labels[3] = "d) ";
labels[4] = "e) ";
String[] options = choices.split("&&");
//Array labels = ["a) ";"b) ";"c) ";"d) ";"e) "];
for(int i = 0; i < options.length; i++)
{
String part = options[i];
System.out.println(labels[i]+part);
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Create help files for the Qt Help Framework