Question
PART A: RESTful web services are an industry standard to communicate with web services. Many web applications you use today use RESTful web and JSON
PART A:
RESTful web services are an industry standard to communicate with web services. Many web applications you use today use RESTful web and JSON to parse the data. This is prevalent on mobile apps.
We will be creating a program that uses a RESTful web service call and JSON to parse the returning information.
Install a Java IDE (either Netbeans or Eclipse). We will be using the RESTful service from services.groupkt.com to find the country code for a particular country. There are several RESTful services you can find. Check out government sites or feel free to use: http://api.geonames.org/postalCodeLookupJSON?postalcode=43207&country=US&username=devry or https://afsl.usgovxml.com/welcome.aspx?loc=43065 for some other options. RESTful web services are a common way to make calls out to other systems (facebook, twitter, etc).
Go to File->New Project and create a new project. If you are using eclipse, create a new class, if Netbeans your class will be created for you. Enter the following code in your Main. This code will ask the user for a country code. Try a few: IN, US, MM, etc and see what you get. The output will be in a format we will discuss next. When entering this code you will need to import several java libraries. If you get an error in your code click on it and you will get a message to import.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s1="http://services.groupkt.com/country/get/iso2code/"; //Site to use RESTful web service call
String s2="";
System.out.println("Please enter the country code: ");
s2 = input.nextLine().toUpperCase(); //Ask the user for the country code
String s3=s1+s2; //concatenate string for the RESTful web service
URL url;
try {
url = new URL(s3);//Set up string as a URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream content = (InputStream) connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content)); //Read data from input stream
String line;
while ((line = in.readLine()) != null) { //Loop and print to console
System.out.println(line);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When you run the code you can enter the country code. I typed in IN and got the following response:
{
"RestResponse" : {
"messages" : [ "Country found matching code [IN]." ],
"result" : {
"name" : "India",
"alpha2_code" : "IN",
"alpha3_code" : "IND"
}
}
}
BUILD SUCCESSFUL (total time: 3 seconds)
Copy
Copy and paste the result from your code below. Choose a different country from IN and see what you get:
PART B:
The format this data is in is a common format for webservice responses. This is JSON format and is an industry standard. To parse JSON format you need to know the fields but often when using web/database you will be pulling data into specific fields. We will use a jar file to parse this data. Adding jar files to a java project is an important skill. Often there will be jar files that will be needed in your project to improve the functionality. Jar files are zipped files of java classes.
Create a new Java Project
Download json.simple http://www.java2s.com/Code/Jar/j/Downloadjsonsimple11jar.htm Unzip it and save it to a location on your computer.
Next you need to add it to your classpath. If you are using Netbeans, right click on Libraries and choose Add JAR/Folder. Navigate to the location of your JAR file and pick OPEN.
If you are using Eclipse, right click on your project name and choose Build path-> Configure Build path. Click the ClassPath and then choose Add External JARs. Then navigate to the location of your JAR file and choose OPEN.
Then add the following code to your MAIN.
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
String s1="http://services.groupkt.com/country/get/iso2code/";
String s2="";
System.out.println("Please enter the country code: ");
s2 = input.nextLine().toUpperCase();
String s3=s1+s2;
URL url;
try {
url = new URL(s3);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream content = (InputStream) connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
//Create and parse JSON object - will contain fields name, alpha2_code and alpha3_code
JSONParser jpar= new JSONParser();
JSONObject obj = (JSONObject)jpar.parse(new InputStreamReader(content, "UTF-8"));
System.out.println("JSON Object: " + obj);
JSONObject restResponse = (JSONObject)obj.get("RestResponse");
JSONObject result = (JSONObject)restResponse.get("result");
if(obj.containsKey("name"))
{
String name = (String)result.get("name");
System.out.println("Name is "+name);
String alpcode2 = (String)result.get("alpha2_code");
System.out.println("Alpha Code 2 is "+alpcode2);
String alpcode3 = (String)result.get("alpha3_code");
System.out.println("Alpha Code 3 is "+alpcode3);
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Screenshot of output:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started