之前都是使用和风API获取天气数据,也可以,但SDK会了以后,更加简单直接。而且功能也比API更强大,代码更简洁。下面是我折腾差不多一周的成果,这些会了之后,获取天气预报,显示城市,天气图标什么的也都能简单的完成。今天先把这个发出去,过两天把其他 的做完了再发。
因为和风的天气代码和json格式不断的变,所以以下代码适用于4.8版SDK,其他版本我找到了不少,基本都不适用。因为我对JAVA几乎不懂,经过自己不断实验、摸索,最后成功。所以代码肯定不好看,很多也是抄别人的,但不是抄一个人的,所以这里表示感谢,但无法一一列举。
以下是全部详细步骤及全部代码,按照我写的来一定能成功。
一、注册和风用户:https://id.qweather.com/#/login
后面验证过程就不讲了。登录后到最下面,“开发者控制台”。
点击进入。看到下面界面。
二、创建应用获取publicID 和key(关键):
点击上图上的:马上创建一个上面的“+”号。打开下图。
点击左右“免费开发版”后,下图:
这个可以随便写。下一步下图:
点击Andriod SDK。下一步,下图:
都是必填项:上面的名称可以随便写。下面的“绑定packageName”内的东西必须与你开发的app的名称一样,据说不一样获取不到数据,我没试过。一般是这种格式:com.example.thirdversionclock 就在andriodmanifest.mxl里面,下图的位置,这个名字在申请的时候可以随便取,后面想改啥改啥,随便改。
下一步就完成了。下面就是我们要的关键的东西:publicID 和key.
三、(andriod stutio3.5)新建一个app,名字最好就上面填的,不要在在上图的编辑里面修改app名字。
第一步选择app基本外观,我选择的是全屏的。
下一步,下图。上面的名称看第二行,写后面的就行了。下面第二个红框是语言,选java. 点finish,完成新建项目。
四、重点:代码部分。
具体参照开发文档。https://dev.qweather.com/docs/android-sdk/
对于小白有点小坑,我把详细步骤写一下。
1、下载SDK文件:Android SDK配置 | 和风天气开发服务
2、添加SDK文件。把上面的文件拷贝到刚才建的那个项目文件夹下
上面位置1为项目视图,位置2显示的是项目保存的路径,默认是在C盘。在项目路径下面找到/app/libs文件夹,把下载的SDK文件拷贝进去。我这个路径是:E:\AndroidStudioProjects\Weather\app\libs,然后在andriod studio里面,上面视图,找到这个文件,并在这个文件上点击右建,弹出菜单如下,点击下面红框内,导入库。
没导入前面没三角标:
导入后前面有三角标:
3、配置Android Manifest 添加权限。文件位置下图:
上面框是andriod视图,app下面的manifests下面就是那个文件,双击打开。添加下面代码:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
意义:
允许连接网络 | android.permission.INTERNET |
访问GPS定位 | android.permission.ACCESS_FINE_LOCATION |
访问设备状态 | android.permission.READ_PHONE_STATE |
访问WIFI状态 | android.permission.ACCESS_WIFI_STATE |
添加位置:
4、引用库。内容:
implementation 'com.squareup.okhttp3:okhttp:3.12.12' implementation 'com.google.code.gson:gson:2.6.2'
位置见下图:andriod视图,下面的build.gradle(module.app),双击打开。
上面的代码和开发文档里面的不一样,按我的写。
其他混淆什么的我就没做了。
下面是主要代码部分:
五、重要:主要文件代码:
1、布局文件全代码:这里没什么好讲的。复制覆盖粘贴就行了。(项目名要相同,不然不行)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context=".FullscreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="@string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_tianqi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天气"
android:textSize="30dp"
android:layout_marginTop="30dp"
android:textColor="#000"
/>
<TextView
android:id="@+id/tv_wendu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="温度"
android:textSize="30dp"
android:layout_marginTop="70dp"
android:textColor="#000"
/>
<TextView
android:id="@+id/tv_fengli"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="风力"
android:textSize="30dp"
android:layout_marginTop="110dp"
android:textColor="#000"/>
<TextView
android:id="@+id/tv_fengxiang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="风向"
android:textSize="30dp"
android:layout_marginTop="150dp"
android:textColor="#000"
/>
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent">
<Button
android:id="@+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button" />
</LinearLayout>
</FrameLayout>
</FrameLayout>
2、java文件关键代码:
public void queryWeather(){
QWeather.getWeatherNow(FullscreenActivity.this, "CN101280603", Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener(){
public static final String TAG="he_feng_now";
@Override
public void onError(Throwable e) {
Log.i(TAG, "onError: ", e);
System.out.println("Weather Now Error:"+new Gson());
}
@Override
public void onSuccess(WeatherNowBean weatherBean) {
//Log.i(TAG, "getWeather onSuccess: " + new Gson().toJson(weatherBean));
System.out.println("获取天气成功: " + new Gson().toJson(weatherBean));
//先判断返回的status是否正确,当status正确时获取数据,若status不正确,可查看status对应的Code值找到原因
if (Code.OK == weatherBean.getCode()) {
WeatherNowBean.NowBaseBean now = weatherBean.getNow();
String tianqi=now.getText();
String wendu=now.getTemp()+"℃";
String fengli=now.getWindScale();
String fengxiang=now.getWindDir();
tv_tianqi.setText("当前天气:"+tianqi);
tv_wendu.setText("当前温度:"+wendu);
tv_fengxiang.setText("风向:"+fengxiang);
tv_fengli.setText("风力:"+fengli+"级");
} else {
//在此查看返回数据失败的原因
Code code = weatherBean.getCode();
System.out.println("失败代码: " + code);
//Log.i(TAG, "failed code: " + code);
}
}
});
};
全部代码:因为是全屏的界面很多代码不是必须的。如果文件名相同也可以直接复制覆盖粘贴。
HeConfig.init("你的publicID", "你的key");这一行引号里的内容要修改成你的。
package com.example.weather;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Unit;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.HeConfig;
import com.qweather.sdk.view.QWeather;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*/
public class FullscreenActivity extends AppCompatActivity {
private TextView tv_tianqi;
private TextView tv_wendu;
private TextView tv_fengxiang;
private TextView tv_fengli;
/**
* Whether or not the system UI should be auto-hidden after
* {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = true;
/**
* If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* Some older devices needs a small delay between UI widget updates
* and a change of the status and navigation bar.
*/
private static final int UI_ANIMATION_DELAY = 300;
private final Handler mHideHandler = new Handler();
private View mContentView;
private final Runnable mHidePart2Runnable = new Runnable() {
@SuppressLint("InlinedApi")
@Override
public void run() {
// Delayed removal of status and navigation bar
// Note that some of these constants are new as of API 16 (Jelly Bean)
// and API 19 (KitKat). It is safe to use them, as they are inlined
// at compile-time and do nothing on earlier devices.
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
};
private View mControlsView;
private final Runnable mShowPart2Runnable = new Runnable() {
@Override
public void run() {
// Delayed display of UI elements
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.show();
}
mControlsView.setVisibility(View.VISIBLE);
}
};
private boolean mVisible;
private final Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
hide();
}
};
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_content_controls);
mContentView = findViewById(R.id.fullscreen_content);
tv_tianqi =(TextView) findViewById(R.id.tv_tianqi);
tv_wendu =(TextView) findViewById(R.id.tv_wendu);
tv_fengxiang =(TextView) findViewById(R.id.tv_fengxiang);
tv_fengli =(TextView) findViewById(R.id.tv_fengli);
HeConfig.init("你的publicID", "你的key");
HeConfig.switchToDevService();
queryWeather();
// Set up the user interaction to manually show or hide the system UI.
mContentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggle();
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
private void toggle() {
if (mVisible) {
hide();
} else {
show();
}
}
private void hide() {
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mControlsView.setVisibility(View.GONE);
mVisible = false;
// Schedule a runnable to remove the status and navigation bar after a delay
mHideHandler.removeCallbacks(mShowPart2Runnable);
mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
}
@SuppressLint("InlinedApi")
private void show() {
// Show the system bar
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
mVisible = true;
// Schedule a runnable to display UI elements after a delay
mHideHandler.removeCallbacks(mHidePart2Runnable);
mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
}
/**
* Schedules a call to hide() in delay milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
public void queryWeather(){
QWeather.getWeatherNow(FullscreenActivity.this, "CN101280603", Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener(){
public static final String TAG="he_feng_now";
@Override
public void onError(Throwable e) {
Log.i(TAG, "onError: ", e);
System.out.println("Weather Now Error:"+new Gson());
}
@Override
public void onSuccess(WeatherNowBean weatherBean) {
//Log.i(TAG, "getWeather onSuccess: " + new Gson().toJson(weatherBean));
System.out.println("获取天气成功: " + new Gson().toJson(weatherBean));
//先判断返回的status是否正确,当status正确时获取数据,若status不正确,可查看status对应的Code值找到原因
if (Code.OK == weatherBean.getCode()) {
WeatherNowBean.NowBaseBean now = weatherBean.getNow();
String tianqi=now.getText();
String wendu=now.getTemp()+"℃";
String fengli=now.getWindScale();
String fengxiang=now.getWindDir();
tv_tianqi.setText("当前天气:"+tianqi);
tv_wendu.setText("当前温度:"+wendu);
tv_fengxiang.setText("风向:"+fengxiang);
tv_fengli.setText("风力:"+fengli+"级");
} else {
//在此查看返回数据失败的原因
Code code = weatherBean.getCode();
System.out.println("失败代码: " + code);
//Log.i(TAG, "failed code: " + code);
}
}
});
};
}
完成后,虚拟运行效果:
默认的全屏界面内容我一点都没改。