You need an API Key to use the OpenWeatherMap API. Head on over to their website if you don't already have one.
allprojects {
repositories {
...
maven { url'https://jitpack.io' }
}
}implementation'com.github.KwabenBerko:OpenWeatherMap-Android-Library:2.1.0'Note: Remember to include the INTERNET permission to your manifest file
OpenWeatherMapHelperhelper = newOpenWeatherMapHelper(getString(R.string.OPEN_WEATHER_MAP_API_KEY));helper.setUnits(Units.IMPERIAL);Units.IMPERIAL (Fahrenheit)Units.METRIC (Celsius)
helper.setLanguage(Languages.ENGLISH);helper.getCurrentWeatherByCityName("Accra", newCurrentWeatherCallback() {
@OverridepublicvoidonSuccess(CurrentWeathercurrentWeather) {
Log.v(TAG, "Coordinates: " + currentWeather.getCoord().getLat() + ", "+currentWeather.getCoord().getLon() +"\n"
+"Weather Description: " + currentWeather.getWeather().get(0).getDescription() + "\n"
+"Temperature: " + currentWeather.getMain().getTempMax()+"\n"
+"Wind Speed: " + currentWeather.getWind().getSpeed() + "\n"
+"City, Country: " + currentWeather.getName() + ", " + currentWeather.getSys().getCountry()
);
}
@OverridepublicvoidonFailure(Throwablethrowable) {
Log.v(TAG, throwable.getMessage());
}
});helper.getCurrentWeatherByCityID("524901", newCurrentWeatherCallback() {
@OverridepublicvoidonSuccess(CurrentWeathercurrentWeather) {
Log.v(TAG, "Coordinates: " + currentWeather.getCoord().getLat() + ", "+currentWeather.getCoord().getLon() +"\n"
+"Weather Description: " + currentWeather.getWeather().get(0).getDescription() + "\n"
+"Temperature: " + currentWeather.getMain().getTempMax()+"\n"
+"Wind Speed: " + currentWeather.getWind().getSpeed() + "\n"
+"City, Country: " + currentWeather.getName() + ", " + currentWeather.getSys().getCountry()
);
}
@OverridepublicvoidonFailure(Throwablethrowable) {
Log.v(TAG, throwable.getMessage());
}
});helper.getCurrentWeatherByGeoCoordinates(5.6037, 0.1870, newCurrentWeatherCallback() {
@OverridepublicvoidonSuccess(CurrentWeathercurrentWeather) {
Log.v(TAG, "Coordinates: " + currentWeather.getCoord().getLat() + ", "+currentWeather.getCoord().getLon() +"\n"
+"Weather Description: " + currentWeather.getWeather().get(0).getDescription() + "\n"
+"Temperature: " + currentWeather.getMain().getTempMax()+"\n"
+"Wind Speed: " + currentWeather.getWind().getSpeed() + "\n"
+"City, Country: " + currentWeather.getName() + ", " + currentWeather.getSys().getCountry()
);
}
@OverridepublicvoidonFailure(Throwablethrowable) {
Log.v(TAG, throwable.getMessage());
}
});helper.getCurrentWeatherByZipCode("90003", newCurrentWeatherCallback() {
@OverridepublicvoidonSuccess(CurrentWeathercurrentWeather) {
Log.v(TAG, "Coordinates: " + currentWeather.getCoord().getLat() + ", "+currentWeather.getCoord().getLon() +"\n"
+"Weather Description: " + currentWeather.getWeather().get(0).getDescription() + "\n"
+"Temperature: " + currentWeather.getMain().getTempMax()+"\n"
+"Wind Speed: " + currentWeather.getWind().getSpeed() + "\n"
+"City, Country: " + currentWeather.getName() + ", " + currentWeather.getSys().getCountry()
);
}
@OverridepublicvoidonFailure(Throwablethrowable) {
Log.v(TAG, throwable.getMessage());
}
});helper.getThreeHourForecastByCityName("Pretoria", newThreeHourForecastCallback() {
@OverridepublicvoidonSuccess(ThreeHourForecastthreeHourForecast) {
Log.v(TAG, "City/Country: "+ threeHourForecast.getCity().getName() + "/" + threeHourForecast.getCity().getCountry() +"\n"
+"Forecast Array Count: " + threeHourForecast.getCnt() +"\n"//For this example, we are logging details of only the first forecast object in the forecasts array
+"First Forecast Date Timestamp: " + threeHourForecast.getList().get(0).getDt() +"\n"
+"First Forecast Weather Description: " + threeHourForecast.getList().get(0).getWeather().get(0).getDescription()+ "\n"
+"First Forecast Max Temperature: " + threeHourForecast.getList().get(0).getMain().getTempMax()+"\n"
+"First Forecast Wind Speed: " + threeHourForecast.getList().get(0).getWind().getSpeed() + "\n"
);
}
@OverridepublicvoidonFailure(Throwablethrowable) {
Log.v(TAG, throwable.getMessage());
}
});helper.getThreeHourForecastByCityID("524901", newThreeHourForecastCallback() {
@OverridepublicvoidonSuccess(ThreeHourForecastthreeHourForecast) {
Log.v(TAG, "City/Country: "+ threeHourForecast.getCity().getName() + "/" + threeHourForecast.getCity().getCountry() +"\n"
+"Forecast Array Count: " + threeHourForecast.getCnt() +"\n"//For this example, we are logging details of only the first forecast object in the forecasts array
+"First Forecast Date Timestamp: " + threeHourForecast.getList().get(0).getDt() +"\n"
+"First Forecast Weather Description: " + threeHourForecast.getList().get(0).getWeather().get(0).getDescription()+ "\n"
+"First Forecast Max Temperature: " + threeHourForecast.getList().get(0).getMain().getTempMax()+"\n"
+"First Forecast Wind Speed: " + threeHourForecast.getList().get(0).getWind().getSpeed() + "\n"
);
}
@OverridepublicvoidonFailure(Throwablethrowable) {
Log.v(TAG, throwable.getMessage());
}
});helper.getThreeHourForecastByGeoCoordinates(6.5244,3.3792, newThreeHourForecastCallback() {
@OverridepublicvoidonSuccess(ThreeHourForecastthreeHourForecast) {
Log.v(TAG, "City/Country: "+ threeHourForecast.getCity().getName() + "/" + threeHourForecast.getCity().getCountry() +"\n"
+"Forecast Array Count: " + threeHourForecast.getCnt() +"\n"//For this example, we are logging details of only the first forecast object in the forecasts array
+"First Forecast Date Timestamp: " + threeHourForecast.getList().get(0).getDt() +"\n"
+"First Forecast Weather Description: " + threeHourForecast.getList().get(0).getWeather().get(0).getDescription()+ "\n"
+"First Forecast Max Temperature: " + threeHourForecast.getList().get(0).getMain().getTempMax()+"\n"
+"First Forecast Wind Speed: " + threeHourForecast.getList().get(0).getWind().getSpeed() + "\n"
);
}
@OverridepublicvoidonFailure(Throwablethrowable) {
Log.v(TAG, throwable.getMessage());
}
});helper.getThreeHourForecastByZipCode("94040", newThreeHourForecastCallback() {
@OverridepublicvoidonSuccess(ThreeHourForecastthreeHourForecast) {
Log.v(TAG, "City/Country: "+ threeHourForecast.getCity().getName() + "/" + threeHourForecast.getCity().getCountry() +"\n"
+"Forecast Array Count: " + threeHourForecast.getCnt() +"\n"//For this example, we are logging details of only the first forecast object in the forecasts array
+"First Forecast Date Timestamp: " + threeHourForecast.getList().get(0).getDt() +"\n"
+"First Forecast Weather Description: " + threeHourForecast.getList().get(0).getWeather().get(0).getDescription()+ "\n"
+"First Forecast Max Temperature: " + threeHourForecast.getList().get(0).getMain().getTempMax()+"\n"
+"First Forecast Wind Speed: " + threeHourForecast.getList().get(0).getWind().getSpeed() + "\n"
);
}
@OverridepublicvoidonFailure(Throwablethrowable) {
Log.v(TAG, throwable.getMessage());
}
});- Hourly Forecast 4 days
- Daily Forecast 16 days