Java, HTML, web service

User Generated

Ybena1997

Programming

Description

Lab 11

Remeinder: This lab is one of the graded labs worth more points.

1. SUMMARY

a.

You will be calling a weather web service that provides current weather info for a given city and

comparing the weather between two cities the user provides.

b. This lab will involve the following new features:

i. Consuming a REST web service.

ii. Utilizing Google’s Gson library to convert json to java objects.

2. DETAILS

a.

Create a normal java application project like we’ve done before.

b. Download the gson jar file provided with this lab. You need to add the jar file to your project like

we've done in the past (see previous postings on how to do this).

c.

First create the java classes that the gson library will fill with incoming json data.

i. These java classes will model the json that is returned from the weather web service.

ii. Here is what the returned json looks like:

{

"coord":{"lon":-116.96,"lat":32.79},

"weather":[

{"id":800,"main":"Clear","description":"clear sky","icon":"02d"}

],

"base":"stations",

"main":{

"temp":71.6,

"pressure":1019,

"humidity":14,

"temp_min":68,

"temp_max":75.2

},

"visibility":16093,

"wind":{"speed":2.95,"deg":234},

"clouds":{"all":5},

"dt":1479581760,

"sys":{

"type":1,

"id":476,

"message":0.1939,

"country":"US",

"sunrise":1479565376,

"sunset":1479602637

},

"id":5345529,

"name":"El Cajon",

"cod":200

}

iii. All we care about is getting the value “name” and values from “main” like “temp” “temp_min”

and “temp_max”.

iv. So we will 2 create java classes to mirror this relationship:

IMPORTANT

: Remember that you can add a package from the File > New menu. All these files go

in “com.grossmont.ws”. When you create a new class, just right click on the package folder you

want to add the class to.

NOTE: You’ll notice that the instance variable names below match the json fields passed back.

That matching is necessary for gson to be able to map the json to your fields.

v. Weather_main.java

1. This is just the “main” object portion of the json that holds the below fields.

2. Package:

com.grossmont.ws

3. Create five public instance variables:

a.

All are

float

type --

temp, pressure, humidity, temp_min, temp_max

i. You’ll notice that in the above json sample, there is a mapping of main and inside main

are these five variable names.

vi. Weather.java

1. This is the root object that holds the top most fields in the returned json.

2. Package:

com.grossmont.ws

3. Create two public instance variables:

a.

main

– this is Weather_main type that you built above.

b.

name

– this is String type.

d.

WeatherServiceManager.java

(this class was provided to you with this lab doc).

i. You need to put this file in the src/com/grossmont/ws folder in your project.

ii. You will fill in code where you see the ######## markings:

1. Simply follow the guidelines in the java file.

iii. You are essentially wiring in the classes you just created to methods that retrieve data from them.

e. EXTRA CREDIT option (5 points)

i. Use these classes in a web site in Tomcat, and use JSP pages to get user input for just one city and

show the three values for that city (current temp, high, low).

ii. Look back to lab 9 as a guide on how to put your class package folders into a .../WEB-INF/classes

folder and how to instantiate and use your class in a JSP page.

iii. Reminder – since we’re using the gson jar file, you’ll need to put this in a .../WEB-INF/lib folder.

iv. You should have two JSP pages:

1. one to get a city from the user in a form which submits those values to the second page.

2. A second page to process the submitted user city.

a.

It needs to import and instantiate your WebServiceManager class.

b. It needs to get the incoming parameter of city.

c.

It needs to take out any spaces (see main method in WebServiceManager class).

d. It needs to display current temp and high & low.

3. TURNING IN LAB:

a.

Turn in the above 3 files into your lab 11 assignment spot in Canvas.

b. If you did the extra credit, also turn in your two jsp pages.

c.

IMPORTANT: DO NOT ZIP your folders/files please

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

At...


Anonymous
Just what I was looking for! Super helpful.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags