Introduction
In this article, we will learn how to create our own weather app using Retrofit 2. There are a lot of providers available to provide weather data. In this tutorial we will use the famous provider “OpenWeatherMap” Api to build our own weather app.
Coding Part
I have arranged the article as follows.
- Step 1 - Creating New Project with Empty Activity.
- Step 2 - Setting up the Retrofit HTTP Library and Manifest.
- Step 3 - Getting App ID from Open Weather API.
- Step 4 - Implementation of consumption of Weather API using Retrofit.
Creating New Project with Android Studio
- Open Android Studio and Select Create new project.
- Name the project as per your wish and select your activity template.

- Click “finish” button to create a new project in Android Studio.
Setting up the Retrofit Http Library and Manifest
In this part, we will see how to set up the library for the project.
- Then add the following lines in app level build.gradle file to apply Google services to your project.
- dependencies {
- ...
- implementation 'com.squareup.retrofit2:retrofit:2.0.0'
- implementation 'com.squareup.retrofit2:converter-gson:2.0.0'
- }
- Then click “Sync Now” to set up your project.
- Don't forget to add the following permission in your manifest file
- <uses-permission android:name="android.permission.INTERNET"/>
Getting App Id from Open Weather Map
- Open OpenWeatherMap Site and sign up for free to get your app id.

- You can find or create your app id in API Keys tab of the site after logged in. Find the screenshot for your reference.

Implementation of consumption of Weather API using Retrofit
The following API link is used to get the current weather report respective to the geo-coordinates. The Sample API link is here.
We will see, how to use the link to access the weather data.
- Create an interface file named as “WeatherService.java” and add the following lines
- public interface WeatherService {
- @GET("data/2.5/weather?")
- Call<WeatherResponse> getCurrentWeatherData(@Query("lat") String lat, @Query("lon") String lon, @Query("APPID") String app_id);
- }
- Create a class file named as “WeatherResponse.java” and add the following lines. We will get the response as json as in the following.
- {
- "coord": {
- "lon": 139,
- "lat": 35
- },
- "sys": {
- "country": "JP",
- "sunrise": 1369769524,
- "sunset": 1369821049
- },
- "weather": [
- {
- "id": 804,
- "main": "clouds",
- "description": "overcast clouds",
- "icon": "04n"
- }
- ],
- "main": {
- "temp": 289.5,
- "humidity": 89,
- "pressure": 1013,
- "temp_min": 287.04,
- "temp_max": 292.04
- },
- "wind": {
- "speed": 7.31,
- "deg": 187.002
- },
- "rain": {
- "3h": 0
- },
- "clouds": {
- "all": 92
- },
- "dt": 1369824698,
- "id": 1851632,
- "name": "Shuzenji",
- "cod": 200
- }
- I have used json to java converter online to generate the Response class and the equivalent java class should be shown below.
- public class WeatherResponse {
- @SerializedName("coord")
- public Coord coord;
- @SerializedName("sys")
- public Sys sys;
- @SerializedName("weather")
- public ArrayList<Weather> weather = new ArrayList<Weather>();
- @SerializedName("main")
- public Main main;
- @SerializedName("wind")
- public Wind wind;
- @SerializedName("rain")
- public Rain rain;
- @SerializedName("clouds")
- public Clouds clouds;
- @SerializedName("dt")
- public float dt;
- @SerializedName("id")
- public int id;
- @SerializedName("name")
- public String name;
- @SerializedName("cod")
- public float cod;
- }
- class Weather {
- @SerializedName("id")
- public int id;
- @SerializedName("main")
- public String main;
- @SerializedName("description")
- public String description;
- @SerializedName("icon")
- public String icon;
- }
- class Clouds {
- @SerializedName("all")
- public float all;
- }
- class Rain {
- @SerializedName("3h")
- public float h3;
- }
- class Wind {
- @SerializedName("speed")
- public float speed;
- @SerializedName("deg")
- public float deg;
- }
- class Main {
- @SerializedName("temp")
- public float temp;
- @SerializedName("humidity")
- public float humidity;
- @SerializedName("pressure")
- public float pressure;
- @SerializedName("temp_min")
- public float temp_min;
- @SerializedName("temp_max")
- public float temp_max;
- }
- class Sys {
- @SerializedName("country")
- public String country;
- @SerializedName("sunrise")
- public long sunrise;
- @SerializedName("sunset")
- public long sunset;
- }
- class Coord {
- @SerializedName("lon")
- public float lon;
- @SerializedName("lat")
- public float lat;
- }

Chinedum EzeozuePosted Jan 1, 2021, 10:23 AM
Hey, you said Create a class file named as “WeatherResponse.java” and add the following lines. We will get the response as json as in the following. BUT YOU DIDN'T SAY WHAT LINES WE SHOULD ADD
📏6️⃣dɛnɪs foʊʃeɪ6️⃣📏Posted Jul 1, 2020, 3:55 PM
Thanks for the tutorial. I just opened the repo in GitHub Android studio and received this error "Unsupported Modules Detected: Compilation is not supported for following modules: app, OpenWeatherApi. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project." Any ideas? TIA
Bob SchneiderPosted Aug 6, 2019, 8:41 AM
Im getting this error what should I do? : java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
Pankaj GuptaPosted Jun 17, 2019, 1:27 PM
I'm getting null value as answer in textview instead of latitude and longitude
davod basiriPosted May 17, 2019, 6:59 AM
Hi we have to make a class with all we will get the response as json ? for exmple i just need temp and speed but must define all parametrs ?
Luciano BalzanoPosted Apr 6, 2019, 8:23 AM
I tried to get weather data by name of city using the schema : http://api.openweathermap.org/data/2.5/find?mode=json&type=like&q=capri,it&appid=, but give me error .