Did you ever think about having an Android application that shows Weather information to users? or Do you have any Android application with the subject of Weather status? I think you would love to check this library out =)))
WeatherNotFound provides everything you need to access the latest status of weather all over the world. also, it will provide you with cache functionalities for a better experience in your application.
To use WeatherNotFound library, you should follow the steps below:
- In
setting.gradlefile of your project, you should addjitpackrepository like the following snippet:
pluginManagement {
repositories {
...
maven("https://jitpack.io")
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
...
maven("https://jitpack.io")
}
}- In
build.gradlefile of your project, you must add the following dependency like the below:
dependencies {
implementation("com.github.AbolfaZlRezaEe:WeatherNotFound:{latest_version}")
}- After you followed the two steps above, you should sync the project and wait for Gradle to download all dependencies it needs. when syncing is finished, you have two options for initializing the library:
- You can initialize the library within the
Manifestfile. The only thing you need is yourOpenWeatherMapApiKey which you recieved from the website. After that in yourManifestfile, you must add these two metadatas like bellow:
<application>
<meta-data
android:name="weather_not_found.auto_init_enabled"
android:value="true"/>
<meta-data
android:name="weather_not_found.open_weather_api_key"
android:value="Your Open Weather ApiKey"/>
</application>- Or maybe you want to initialize the library with a lot more configurations. For that, first make sure you disabled auto init mechanism in
Manifestfile like bellow:
<application>
<meta-data
android:name="weather_not_found.auto_init_enabled"
android:value="false"/>
</application>And then, initialize the library using init function like bellow:
class BaseApplication : Application() {
override fun onCreate() {
super.onCreate()
WeatherNotFound.getInstance().init(
context = this,
openWeatherApiKey = /* Your value */,
openWeatherResponseLanguage = /* Your value */,
openWeatherResponseUnit = /* Your value */,
httpLoggingLevel = HttpLoggingInterceptor.Level.BODY,
cacheMechanismEnabled = true,
readTimeoutInSeconds = /* Your value */,
connectTimeoutInSeconds = /* Your value */
)
}
}- Finally this is the time you can use the library! in your activity, you can use OpenWeatherMap services like this:
NOTE: For now, we just have CurrentWeather Api and 5 Day 3 Hour Forecast! Other APIs will be added as soon as possible.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
WeatherNotFound.getInstance().getCurrentWeatherInformation(
latitude = latitude,
longitude = longitude,
weatherNotFoundCallback = object :
WeatherNotFoundCallback<WeatherNotFoundResponse<CurrentWeatherModel>, WeatherNotFoundError> {
override fun onSuccess(response: WeatherNotFoundResponse<CurrentWeatherModel>) {
// Do whatever you want...
}
override fun onError(error: WeatherNotFoundError) {
error.exception?.printStackTrace()
}
}
)
}
}For more information you can see Sample Application!
In some situations, WeatherNotFound will log several errors which tells you what the problem is. for better understanding, we described the reason for each error in following section:
-
Please ensure you have INTERNET permission in your application!: This run-time exception will occur when you don't have Internet permission on your application. make sure you added this permission in theAndroidManifest.xmlfile. -
You didn't call init() function of WeatherNotFound!: This run-time exception will occur when you don't call theinit()function of WeatherNotFound library in yourApplicationclass. make sure you call it! -
Validation failed! Your Api key is not working...: This is a log that you might see in your logcat section of Android Studio. if you are running Sample Application and saw this, you should checkREADME.mdfile of Sample Application module and follow the steps correctly. If you are using the library in your own application, please make sure you followed these instructions correctly! -
You are not allowed to call init() function if auto-init mechanism enabled!: This run-time exception will occur when you don't disable auto-init mechanism and also callinitfunction of library in code! Please either disable the auto-init withmeta-datatag inManifestfile, or removeinitfunction from the code!
Don't forget that if you just created your Apikey, it takes some time to enabling it from OpenWeatherMap. So wait some minutes and then try again.
Make me happy by contributing to this project! You can help me fix bugs, add features and resolve issues so WeatherNotFound can grow. To start your contribution, submit new issues and create pull requests. You can also check out the list of problems in the Issues Section.
