Professional Documents
Culture Documents
Android How To Program 3Rd Edition Deitel Solutions Manual Full Chapter PDF
Android How To Program 3Rd Edition Deitel Solutions Manual Full Chapter PDF
0134444302 9780134444307
Download solution manual at:
https://testbankpack.com/p/solution-manual-for-android-how-to-
program-3rd-edition-deitel-0134444302-9780134444307/
7 WeatherViewer App
Objectives
In this chapter you’ll:
■ Use the free
OpenWeatherMap.org
REST web services to get a
16-day weather forecast for a
city specified by the user.
■ Use an AsyncTask and an
HttpUrlConnection to
invoke a REST web service or
to download an image in a
separate thread and deliver
results to the GUI thread.
■ Process a JSON response
using package org.json
classes JSONObjects and
JSONArrays.
■ Define an ArrayAdapter
that specifies the data to
display in a ListView.
■ Use the ViewHolder
pattern to reuse views that
scroll off the screen in a
ListView, rather than
creating new views.
■ Use the material design
components
TextInputLayout,
Snackbar and
FloatingActionButton
from the Android Design
Support Library.
2 Chapter 7 WeatherViewer 7.2
App Test-Driving the WeatherViewer App 2
7.1 Introduction
The WeatherViewer app (Fig. 7.1) uses the free OpenWeatherMap.org REST web services
to obtain a specified city’s 16-day weather forecast. The app receives the weather data in
JSON (JavaScript Object Notation) data format. The list of weather data is displayed in a
ListView—a view that displays a scrollable list of items. In this app, you’ll use a custom
list-item format to display:
• a weather-condition icon
• the day of the week with a text description of that day’s weather
• the day’s low and high temperatures (in °F), and
• the humidity percentage.
The preceding items represent a subset of the returned forecast data. For details of the data
returned by the 16-day weather forecast API, visit:
http://openweathermap.org/forecast16
Fig. 7.1 | Weather Viewer app displaying the New York, NY, US weather forecast.
entered the city, touch the circular FloatingActionButton containing the done icon
( ) to submit the city to the app, which then requests that city’s 16-day weather forecast
(shown in Fig. 7.1).
For more information about the license terms, see the Licenses section at
http://openweathermap.org/terms
After registering, copy the hexadecimal API key from the confirmation web page, then re-
place YOUR_API_KEY in strings.xml with the key.
Each property name is a String. Arrays are represented in JSON with square brackets in
the following format:
[value1, value2, value3]
Each array element can be a String, number, JSON object, true, false or null.
Figure 7.3 sample JSON returned by OpenWeatherMap.org’s daily forecast web service
used in this app—this particular sample contains two days of weather data (lines 15–57).
1 {
2 "city": {
3 "id": 5128581,
4 "name": "New York",
5 "coord": {
6 "lon": -74.005966,
7 "lat": 40.714272
8 },
9 "country": "US",
10 "population": 0
11 },
12 "cod": "200",
13 "message": 0.0102,
14 "cnt": 2,
15 "list": [{ // you'll use this array of objects to get the daily weather
16 "dt": 1442419200,
17 "temp": {
18 "day": 79.9,
19 "min": 71.74,
20 "max": 82.53,
21 "night": 71.85,
22 "eve": 82.53,
23 "morn": 71.74
24 },
25 "pressure": 1037.39,
26 "humidity": 64,
27 "weather": [{
28 "id": 800,
29 "main": "Clear",
30 "description": "sky is clear",
31 "icon": "01d"
32 }],
Fig. 7.3 | Sample JSON from the OpenWeatherMap.org daily forecast web service. (Part 1 of 2.)
33 "speed": 0.92,
34 "deg": 250,
35 "clouds": 0
36 }, { // end of first array element and beginning of second one
37 "dt": 1442505600,
38 "temp": {
39 "day": 79.92,
40 "min": 66.72,
41 "max": 83.1,
42 "night": 70.79,
43 "eve": 81.99,
44 "morn": 66.72
45 },
46 "pressure": 1032.46,
47 "humidity": 62,
48 "weather": [{
49 "id": 800,
50 "main": "Clear",
51 "description": "sky is clear",
52 "icon": "01d"
53 }],
54 "speed": 1.99,
55 "deg": 224,
56 "clouds": 0
57 }] // end of second array element and end of array
58 }
Fig. 7.3 | Sample JSON from the OpenWeatherMap.org daily forecast web service. (Part 2 of 2.)
There are many properties in the JSON object returned by the daily forecast. We use
only the "list" property—an array of JSON objects representing the forecasts for up to
16 days (7 by default, unless you specify otherwise). Each "list" array element contains
many properties of which we use:
• "dt"—a long integer containing the date/time stamp represented as the number
of seconds since January 1, 1970 GMT. We convert this into a day name.
• "temp"—a JSON object containing double properties representing the day’s
temperatures. We use only the minimum ("min") and maximum ("max") tem-
peratures, but the web service also returns the average daytime ("day"), nighttime
("night"), evening ("eve") and morning ("morn") temperatures.
• "humidity"—an int representing the humidity percentage.
• "weather"—a JSON object containing several properties, including a descrip-
tion of the conditions ("description") and the name of an icon that represents
the conditions ("icon").
org.json Package
You’ll use the following classes from the org.json package to process the JSON data that
the app receives (Section 7.7.6):
to their corresponding values. You access the JSON properties in your code via
JSONObject’s get methods, which enable you to obtain a JSON key’s value as one
of the types JSONObject, JSONArray, Object, boolean, double, int, long or
String.
• JSONArray—This class represents a JSON array and provides methods for access-
ing its elements. The "list" property in the OpenWeatherMap.org response will
be manipulated as a JSONArray.