OpenWeatherMap-Android-Library


Source link: https://github.com/KwabenBerko/OpenWeatherMap-Android-Library

OpenWeatherMap-Android-Library

You need an API Key to use the OpenWeatherMap API. Head on over to their website if you don't already have one.

Download

Step 1. Add the JitPack repository to your root build.gradle file.

allprojects {

repositories {

  ...
  maven {
 url 'https://jitpack.io' 
}

}
 
}

Step 2 : Download via Gradle:

compile 'com.github.KwabenBerko:OpenWeatherMap-Android-Library:v1.1.1'

Note: Remember to include the INTERNET permission to your manifest file

Usage

Instantiate the OpenWeatherMapHelper class

OpenWeatherMapHelper helper = new OpenWeatherMapHelper();

Set your API Key (Required)

helper.setApiKey(getString(R.string.OPEN_WEATHER_MAP_API_KEY));

Set your Units (Optional, Kelvin by default)

helper.setUnits(Units.IMPERIAL);
Unit Options:
  1. Units.IMPERIAL (Fahrenheit)

  2. Units.METRIC (Celsius)

Features

(1) Current Weather

Get current weather by City Name:

helper.getCurrentWeatherByCityName("Accra", new OpenWeatherMapHelper.CurrentWeatherCallback() {

 @Override

 public void onSuccess(CurrentWeather currentWeather) {

  Log.v(TAG,

 "Coordinates: " + currentWeather.getCoord().getLat() + ", "+currentWeather.getCoord().getLat() +"\n"

 +"Weather Description: " + currentWeather.getWeatherArray().get(0).getDescription() + "\n"

 +"Max Temperature: " + currentWeather.getMain().getTempMax()+"\n"

 +"Wind Speed: " + currentWeather.getWind().getSpeed() + "\n"

 +"City, Country: " + currentWeather.getName() + ", " + currentWeather.getSys().getCountry()

 );

 
}

  @Override

 public void onFailure(Throwable throwable) {

  Log.v(TAG, throwable.getMessage());

 
}

}
);

Get current weather by City ID:


  helper.getCurrentWeatherByCityID("524901", new OpenWeatherMapHelper.CurrentWeatherCallback() {

 @Override

 public void onSuccess(CurrentWeather currentWeather) {

  Log.v(TAG,

 "Coordinates: " + currentWeather.getCoord().getLat() + ", "+currentWeather.getCoord().getLat() +"\n"

 +"Weather Description: " + currentWeather.getWeatherArray().get(0).getDescription() + "\n"

 +"Max Temperature: " + currentWeather.getMain().getTempMax()+"\n"

 +"Wind Speed: " + currentWeather.getWind().getSpeed() + "\n"

 +"City, Country: " + currentWeather.getName() + ", " + currentWeather.getSys().getCountry()

 );

 
}

  @Override

 public void onFailure(Throwable throwable) {

  Log.v(TAG, throwable.getMessage());

 
}

}
);
 

Get current weather by Geographic Coordinates:

helper.getCurrentWeatherByGeoCoordinates(5.6037, 0.1870, new OpenWeatherMapHelper.CurrentWeatherCallback() {

 @Override

 public void onSuccess(CurrentWeather currentWeather) {

  Log.v(TAG,

 "Coordinates: " + currentWeather.getCoord().getLat() + ", "+currentWeather.getCoord().getLat() +"\n"

 +"Weather Description: " + currentWeather.getWeatherArray().get(0).getDescription() + "\n"

 +"Max Temperature: " + currentWeather.getMain().getTempMax()+"\n"

 +"Wind Speed: " + currentWeather.getWind().getSpeed() + "\n"

 +"City, Country: " + currentWeather.getName() + ", " + currentWeather.getSys().getCountry()

 );

 
}

  @Override

 public void onFailure(Throwable throwable) {

  Log.v(TAG, throwable.getMessage());

 
}

}
);
 

Get current weather by Zip Code:

helper.getCurrentWeatherByZipCode("90003", new OpenWeatherMapHelper.CurrentWeatherCallback() {

 @Override

 public void onSuccess(CurrentWeather currentWeather) {

  Log.v(TAG,

 "Coordinates: " + currentWeather.getCoord().getLat() + ", "+currentWeather.getCoord().getLat() +"\n"

 +"Weather Description: " + currentWeather.getWeatherArray().get(0).getDescription() + "\n"

 +"Max Temperature: " + currentWeather.getMain().getTempMax()+"\n"

 +"Wind Speed: " + currentWeather.getWind().getSpeed() + "\n"

 +"City, Country: " + currentWeather.getName() + ", " + currentWeather.getSys().getCountry()

 );

 
}

  @Override

 public void onFailure(Throwable throwable) {

  Log.v(TAG, throwable.getMessage());

 
}

}
);
 

(2) 5 day / 3 hour forecast

Get three hour forecast by City Name:

helper.getThreeHourForecastByCityName("Pretoria", new OpenWeatherMapHelper.ThreeHourForecastCallback() {

 @Override

 public void onSuccess(ThreeHourForecast threeHourForecast) {

  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.getThreeHourWeatherArray().get(0).getDt() +"\n"

+"First Forecast Weather Description: " + threeHourForecast.getThreeHourWeatherArray().get(0).getWeatherArray().get(0).getDescription()+ "\n"

+"First Forecast Max Temperature: " + threeHourForecast.getThreeHourWeatherArray().get(0).getMain().getTempMax()+"\n"

+"First Forecast Wind Speed: " + threeHourForecast.getThreeHourWeatherArray().get(0).getWind().getSpeed() + "\n"

  );

 
}

  @Override

 public void onFailure(Throwable throwable) {

  Log.v(TAG, throwable.getMessage());

 
}

}
);

Get three hour forecast by City ID:

helper.getThreeHourForecastByCityID("524901", new OpenWeatherMapHelper.ThreeHourForecastCallback() {

 @Override

 public void onSuccess(ThreeHourForecast threeHourForecast) {

  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.getThreeHourWeatherArray().get(0).getDt() +"\n"

+"First Forecast Weather Description: " + threeHourForecast.getThreeHourWeatherArray().get(0).getWeatherArray().get(0).getDescription()+ "\n"

+"First Forecast Max Temperature: " + threeHourForecast.getThreeHourWeatherArray().get(0).getMain().getTempMax()+"\n"

+"First Forecast Wind Speed: " + threeHourForecast.getThreeHourWeatherArray().get(0).getWind().getSpeed() + "\n"

  );

 
}

  @Override

 public void onFailure(Throwable throwable) {

  Log.v(TAG, throwable.getMessage());

 
}

}
);
 

Get three hour forecast by Geographic Coordinates:

helper.getThreeHourForecastByGeoCoordinates(6.5244,3.3792, new OpenWeatherMapHelper.ThreeHourForecastCallback() {

 @Override

 public void onSuccess(ThreeHourForecast threeHourForecast) {

  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.getThreeHourWeatherArray().get(0).getDt() +"\n"

+"First Forecast Weather Description: " + threeHourForecast.getThreeHourWeatherArray().get(0).getWeatherArray().get(0).getDescription()+ "\n"

+"First Forecast Max Temperature: " + threeHourForecast.getThreeHourWeatherArray().get(0).getMain().getTempMax()+"\n"

+"First Forecast Wind Speed: " + threeHourForecast.getThreeHourWeatherArray().get(0).getWind().getSpeed() + "\n"

  );

 
}

  @Override

 public void onFailure(Throwable throwable) {

  Log.v(TAG, throwable.getMessage());

 
}

}
);

Get three hour forecast by Zip Code:

helper.getThreeHourForecastByZipCode("94040", new OpenWeatherMapHelper.ThreeHourForecastCallback() {

 @Override

 public void onSuccess(ThreeHourForecast threeHourForecast) {

  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.getThreeHourWeatherArray().get(0).getDt() +"\n"

+"First Forecast Weather Description: " + threeHourForecast.getThreeHourWeatherArray().get(0).getWeatherArray().get(0).getDescription()+ "\n"

+"First Forecast Max Temperature: " + threeHourForecast.getThreeHourWeatherArray().get(0).getMain().getTempMax()+"\n"

+"First Forecast Wind Speed: " + threeHourForecast.getThreeHourWeatherArray().get(0).getWind().getSpeed() + "\n"

  );

 
}

  @Override

 public void onFailure(Throwable throwable) {

  Log.v(TAG, throwable.getMessage());

 
}

}
);
 

Upcoming Feature

  1. 16 day / daily forecast

Resources

Easy splash screen library allows you to generate beautiful and customizable Splash screens with less effort.

A library that allows you to enable/disable features and/or part of code regarding a Context.

Adapter library for RecyclerView to display your app's OSS dependencies.

Estimates the size of a Google Play patch and the new gzipped APK.

Custom animation for Google Speech Recognizer.

Android DialogFragment that enables Dialog to be swiped away to dismiss.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes