CPS251
Android Development
Building the WeatherResponse Models
The WeatherResponse file contains Kotlin data classes that represent the JSON returned by OpenWeatherMap. These classes let Retrofit and Gson map JSON fields into Kotlin objects automatically.
data class WeatherResponse(
val coord: Coord,
val weather: List,
val base: String,
val main: Main,
val visibility: Int,
val wind: Wind,
val clouds: Clouds,
val dt: Long,
val sys: Sys,
val timezone: Int,
val id: Int,
val name: String,
val cod: Int
)
data class Coord(
val lon: Double,
val lat: Double
)
data class Weather(
val id: Int,
val main: String,
val description: String,
val icon: String
)
data class Main(
val temp: Double,
val feels_like: Double,
val temp_min: Double,
val temp_max: Double,
val pressure: Int,
val humidity: Int,
val sea_level: Int?,
val grnd_level: Int?
)
data class Wind(
val speed: Double,
val deg: Int,
val gust: Double?
)
data class Clouds(
val all: Int
)
data class Sys(
val type: Int?,
val id: Int?,
val country: String,
val sunrise: Long,
val sunset: Long
)
Code Explanation
Why Data Classes?
- A
data classis great for storing structured data from APIs. - Kotlin automatically gives useful features like
toString(),equals(), andcopy(). - These classes are easy to read and perfect for JSON mapping.
Top-Level Response Object
WeatherResponseis the full response from the endpoint.- It combines nested objects like
main,wind, andsys. - It also includes simple values such as city name (
name) and status code (cod).
Nested Models
Coordholds location coordinates (lonandlat).Weatherholds weather condition details, and appears as a list because the API returns an array.Mainholds temperature and pressure values.Wind,Clouds, andSyseach represent a specific part of the JSON.
Nullable Fields
- Some fields are optional in the API response, so they use nullable types like
Int?orDouble?. - Examples include
sea_level,grnd_level, andgust. - Nullable types help prevent crashes when a field is missing.
Matching JSON Keys
- Field names in these classes match the JSON keys exactly, including names like
feels_like. - When names match, Gson can map data without extra annotations.
- If names did not match, you would use annotations like
@SerializedName.
How the UI Uses This Model
- The repository returns a
WeatherResponseobject from the API call. - The ViewModel stores it in state.
- The UI reads fields like
weatherResponse.main.tempandweatherResponse.weather.firstOrNull()?.description. - Strong typing makes these accesses safer than manual JSON parsing.
Learning Aids
Tips for Success
- Start by modeling only the fields your app needs, then add more later if needed.
- Use nullable types for fields that may not always be returned.
- Keep model classes small and focused on data only.
- Group related nested JSON objects into separate data classes.
- Check sample JSON from the API docs while building these models.
Common Mistakes to Avoid
- Using non-null types for optional fields, which can cause parsing failures.
- Misspelling field names so they no longer match JSON keys.
- Treating array fields (like
weather) as single objects. - Putting business logic into data model classes.
- Ignoring units and value meaning (for example, metric vs imperial temperature values).
Best Practices
- Keep response models in a dedicated package, such as
apiormodel. - Use clear type choices (
Double,Int,Long) based on API docs. - Prefer immutable properties with
valfor API responses. - Handle nulls in the ViewModel or UI with safe calls and defaults.
- Update your models whenever API response formats change.