Code Monkey home page Code Monkey logo

openweathermap-android-library's Introduction

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:

implementation 'com.github.KwabenBerko:OpenWeatherMap-Android-Library:2.1.0'

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

Usage

Instantiate Class With Your OpenWeatherMap Api Key

OpenWeatherMapHelper helper = new OpenWeatherMapHelper(getString(R.string.OPEN_WEATHER_MAP_API_KEY));

Set your Units (Optional, STANDARD by default)

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

  2. Units.METRIC (Celsius)

Set your Language (ENGLISH by default)

helper.setLanguage(Languages.ENGLISH);

Features

(1) Current Weather

Get current weather by City Name:

 helper.getCurrentWeatherByCityName("Accra", new CurrentWeatherCallback() {
     @Override
     public void onSuccess(CurrentWeather currentWeather) {
         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()
         );
     }

     @Override
     public void onFailure(Throwable throwable) {
         Log.v(TAG, throwable.getMessage());
     }
 });

Get current weather by City ID:

 helper.getCurrentWeatherByCityID("524901", new CurrentWeatherCallback() {
     @Override
     public void onSuccess(CurrentWeather currentWeather) {
         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()
         );
     }

     @Override
     public void onFailure(Throwable throwable) {
         Log.v(TAG, throwable.getMessage());
     }
 });

Get current weather by Geographic Coordinates:

 helper.getCurrentWeatherByGeoCoordinates(5.6037, 0.1870, new CurrentWeatherCallback() {
     @Override
     public void onSuccess(CurrentWeather currentWeather) {
         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()
         );
     }

     @Override
     public void onFailure(Throwable throwable) {
         Log.v(TAG, throwable.getMessage());
     }
 });

Get current weather by Zip Code:

 helper.getCurrentWeatherByZipCode("90003", new CurrentWeatherCallback() {
     @Override
     public void onSuccess(CurrentWeather currentWeather) {
         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()
         );
     }

     @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 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.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"
         );
     }

     @Override
     public void onFailure(Throwable throwable) {
         Log.v(TAG, throwable.getMessage());
     }
 });

Get three hour forecast by City ID:

 helper.getThreeHourForecastByCityID("524901", new 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.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"
         );
     }

     @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 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.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"
         );
     }

     @Override
     public void onFailure(Throwable throwable) {
         Log.v(TAG, throwable.getMessage());
     }
 });

Get three hour forecast by Zip Code:

 helper.getThreeHourForecastByZipCode("94040", new 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.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"
         );
     }

     @Override
     public void onFailure(Throwable throwable) {
         Log.v(TAG, throwable.getMessage());
     }
 });

Upcoming Features

  1. Hourly Forecast 4 days
  2. Daily Forecast 16 days

openweathermap-android-library's People

Contributors

kwabenberko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

openweathermap-android-library's Issues

weather auto update

Does the weather update from OpenWeatherMap automatically in this library? Or do I need to implement something from my end? Thanks

Didn't working in Release version of the app

I had used this library and it is working great in debug mode. but when i create release version, it is crashing. it says java.lang.NullPointerException: throw with null exception in (SourceFile:299). Any idea why is that?

Wind direction

Hello,
Thanks for the library. Can we get the other things like wind direction, sunrise sunset time etc.

Day or 12 hour forecast

Hi!
Thanks for your library. I use it in my small project. Your library works fine.
But I have one question: is it possible to somehow get the weather not for 3 hours, but for the whole day or 12 hours?
For example, in the morning to see and know the weather at lunchtime and in the late afternoon.

Targeting targetSdkVersion 28 prevents weather from showing

Hey when i target sdk version 28 within my app....openweatherhelper does not show the weather within my app, i get no errors.

i must target sdk version 27 or lower in order for your library to work and i do not why this happens.

Thanks

Read weather for next 5 days

How can read the weather for next 5 days?

I try to get the icon for each day using

int icon = ThreeHourForecast.getList().get(i).getWeatherArray().get(0).getId().intValue();

but icons codes are different for what I see on openweathermap site.

In same time when I use ThreeHourForecast.getList().get(i).getMain().getTempMin() or getTempMax() return different temperatures than openweathermap forcast page for same location.

Automatic Weather Updates Feature?

Hello everyone,
Lately, I have been thinking of adding a new feature to the library because of this issue: An Automatic Updates Feature. Wanted to find out from everyone if its a feature that would be beneficial to you.
Kindly let me know what you think.

Language support

Hi,
Nice lib ! But you didn't provide any support for language ?
Thx, BR, Bastien.

FYI : I added a PR #3

Gson issue?

Hi!
I can't figure out the source of this issue - is it on my side or on the library's?
I've recently migrated my project to AndroidX. Maybe this could cause this issue? (the weather works in emulator but not on real devices)

`09-08 00:30:23.922 31040 31040 V FCC_Service: updateWeather FAILURE = Unable to invoke no-args constructor for class e.f.a.b.a.a. Registering an InstanceCreator with Gson for this type may fix this problem.

09-08 00:30:23.922 31040 31040 W System.err: java.lang.RuntimeException: Unable to invoke no-args constructor for class e.f.a.b.a.a. Registering an InstanceCreator with Gson for this type may fix this problem.

09-08 00:30:23.922 31040 31040 W System.err: at com.google.gson.internal.ConstructorConstructor$14.a(ConstructorConstructor.java:228)

09-08 00:30:23.922 31040 31040 W System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.a(ReflectiveTypeAdapterFactory.java:212)

09-08 00:30:23.922 31040 31040 W System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.a(ReflectiveTypeAdapterFactory.java:131)

09-08 00:30:23.922 31040 31040 W System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.a(ReflectiveTypeAdapterFactory.java:222)

09-08 00:30:23.922 31040 31040 W System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)

09-08 00:30:23.922 31040 31040 W System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)

09-08 00:30:23.922 31040 31040 W System.err: at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:122)

09-08 00:30:23.922 31040 31040 W System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:217)

09-08 00:30:23.922 31040 31040 W System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:116)

09-08 00:30:23.932 31040 31040 W System.err: at com.google.firebase.perf.network.zzg.onResponse(Unknown Source)

09-08 00:30:23.932 31040 31040 W System.err: at i.H$a.b(RealCall.java:153)

09-08 00:30:23.932 31040 31040 W System.err: at i.a.b.run(NamedRunnable.java:32)

09-08 00:30:23.932 31040 31040 W System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

09-08 00:30:23.932 31040 31040 W System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

09-08 00:30:23.932 31040 31040 W System.err: at java.lang.Thread.run(Thread.java:841)

09-08 00:30:23.932 31040 31040 W System.err: Caused by: java.lang.UnsupportedOperationException: Abstract class can't be instantiated! Class name: e.f.a.b.a.a

09-08 00:30:23.932 31040 31040 W System.err: at com.google.gson.internal.UnsafeAllocator.a(UnsafeAllocator.java:120)

09-08 00:30:23.932 31040 31040 W System.err: at com.google.gson.internal.UnsafeAllocator$1.b(UnsafeAllocator.java:49)

09-08 00:30:23.932 31040 31040 W System.err: at com.google.gson.internal.ConstructorConstructor$14.a(ConstructorConstructor.java:225)

09-08 00:30:23.932 31040 31040 W System.err: ... 14 more`

License?

We would like to give credit for the library in Lawnchair Launcher, but we can't find any LICENSE file in your repository or any related information in the readme.

Under what license is your work licensed?

5 day forecast

In your README is a "5 day / 3 hour forecast" provided, but I can only see an example for a 3 hour forecast, so how can I use the 5 day forecast?

Sunrise/Sunset

Hi,

getSunrise() & getSunset() are returning the wrong values, it looks like you are getting the data in JSON format, I think these are only returned when using XML, can you confirm & is there a way to correct it?

Getting data on precipitation

Hello,
The library you wrote is simply wonderful :)
I just wanted to ask,
how can I get the data on precipitation out of the weather forecast (value, unit and type)?

Thx in advance :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.