Set up Android SDK
This document will introduce how to configure the Android SDK for QWeather API.
OS requirement:
Android 8.0+, minSDK 21
Step 1: Create project and credential
Make sure you have created a Project and Credential, see Project and KEY and JWT authentication.
Step 2: Install SDK
Download the latest SDK: QWeather Android SDK 5.0.0 (MD5:d652f6fd3c61ce02595dac34b16a18a5
)
Copy the JAR file to the app/libs/
YOUR-PROJECT/
├── app/
│ ├── libs/
│ │ └── QWeather_Public_Android_V5.0.0.jar
│ ├── src/
│ └── build.gradle
Update the Gradle configuration app/build.gradle
dependencies {
// add QWeather jar
implementation files('libs/QWeather_Public_Android_V5.0.0.jar')
// or add all jar
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
Reference library
implementation libs.gson
implementation libs.okhttp
Add the following code to the obfuscation file proguard-rules.pro
// exclude okhttp
-dontwarn com.squareup.**
-dontwarn okio.**
-keep public class org.codehaus.* { *; }
-keep public class java.nio.* { *; }
// exclude QWeather
-dontwarn com.qweather.sdk.**
-keep class com.qweather.sdk.** { *;}
Step 3: Add API Host and token
Hint: iOS SDK only support JWT for authentication.
QWeather.getInstance(MainActivity.this, "{YOUR_HOST}") // Initialize api host
.setTokenGenerator(new TokenGenerator() {
@Override
public String generator() {
// Update jwt token should be implemented here in production environments
return "{YOUR_TOKEN}";
}
});
Sample code
Get real-time weather for Beijing.
WeatherParameter parameter = new WeatherParameter("101010100")
.lang(Lang.ZH_HANS)
.unit(Unit.METRIC);
QWeather.instance.weatherNow(parameter, new Callback<WeatherNowResponse>() {
@Override
public void onSuccess(WeatherNowResponse response) {
Log.i(TAG, response.toString());
}
@Override
public void onFailure(ErrorResponse errorResponse) {
Log.i(TAG,errorResponse.toString());
}
@Override
public void onException(Throwable e) {
e.printStackTrace();
}
});