? ? ? ? 接入接口前首先申请应用密钥Key,登录高德开发者开放平台,创建应用,获取密钥。
? ? ? ? ?天气查询API服务地址:https://restapi.amap.com/v3/weather/weatherInfo?parameters,需以Get请求方式调用,parameters代表所有参数,参数以&进行分隔。
? ? ? ? 前两个参数为必填参数,extensions传入base代表实况天气,all代表预报天气,定义枚举用以区分:
public enum GetDataType
{
/// <summary>
/// 获取实况天气
/// </summary>
Lives,
/// <summary>
/// 获取预报天气
/// </summary>
Forecast
}
????????这里我们以JSON格式解析接口响应数据,所以output传入JSON。最终封装Weather天气类:
using System;
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
[AddComponentMenu("")]
public class Weather : MonoBehaviour
{
private static Weather instance;
public static Weather Instance
{
get
{
if (instance == null)
{
instance = new GameObject("[Weather]").AddComponent<Weather>();
DontDestroyOnLoad(instance);
}
return instance;
}
}
//密钥 于高德开发者平台创建应用申请获得
private const string key = "3ab38063f3ffc3d7047f984f2822711a";
public enum GetDataType
{
/// <summary>
/// 获取实况天气
/// </summary>
Lives,
/// <summary>
/// 获取预报天气
/// </summary>
Forecast
}
/// <summary>
/// 获取天气数据
/// </summary>
/// <param name="city">城市编码</param>
/// <param name="callback">回调函数</param>
public void Get(string city, GetDataType type, Action<string> callback)
{
StartCoroutine(SendWebRequest(city, type, callback));
}
private IEnumerator SendWebRequest(string city, GetDataType type, Action<string> callback)
{
//url拼接
string url = string.Format("https://restapi.amap.com/v3/weather/weatherInfo?key={0}&city={1}&extensions={2}", key, city, type == GetDataType.Lives ? "base" : "all");
//GET方式调用API服务
using (UnityWebRequest request = UnityWebRequest.Get(url))
{
DateTime beginTime = DateTime.Now;
yield return request.SendWebRequest();
DateTime endTime = DateTime.Now;
if (request.result == UnityWebRequest.Result.Success)
{
Debug.Log($"{beginTime} 发起网络请求 于 {endTime} 收到响应:\r\n{request.downloadHandler.text}");
callback.Invoke(request.downloadHandler.text);
}
else
{
Debug.Log($"发起网络请求失败:{request.error}");
}
}
}
private void OnDestroy()
{
instance = null;
}
}
????????调用实况天气数据测试(320115代表南京市江宁区,具体城市区域编码参考城市编码表,于高德开放平台下载):
Weather.Instance.Get("320115", Weather.GetDataType.Lives, data => { });
?????????调用预测天气数据测试:
Weather.Instance.Get("320115", Weather.GetDataType.Forecast, data => { });
? ? ? ? ?最终运用接口响应的数据,需要定义响应数据结构,将字符串数据反序列化为我们定义的类,参数说明:
using System;
[Serializable]
/// <summary>
/// 天气API响应数据结构
/// </summary>
public class WeatherResponse
{
/// <summary>
/// 返回状态 1成功/0失败
/// </summary>
public int status;
/// <summary>
/// 返回结果总数目
/// </summary>
public int count;
/// <summary>
/// 返回的状态信息
/// </summary>
public string info;
/// <summary>
/// 返回状态说明 10000代表正确
/// </summary>
public int infoCode;
/// <summary>
/// 实况天气数据信息
/// </summary>
public WeatherLive[] lives;
/// <summary>
/// 预报天气信息数据
/// </summary>
public WeatherForecast[] forecast;
}
[Serializable]
/// <summary>
/// 实况天气数据
/// </summary>
public class WeatherLive
{
/// <summary>
/// 省份名
/// </summary>
public string province;
/// <summary>
/// 城市名
/// </summary>
public string city;
/// <summary>
/// 区域编码
/// </summary>
public string adcode;
/// <summary>
/// 天气现象(汉字描述)
/// </summary>
public string weather;
/// <summary>
/// 实时气温 单位:摄氏度
/// </summary>
public int temperature;
/// <summary>
///风向描述
/// </summary>
public string winddirection;
/// <summary>
/// 风力级别 单位:级
/// </summary>
public int windpower;
/// <summary>
/// 空气适度
/// </summary>
public int humidity;
/// <summary>
/// 数据发布时间
/// </summary>
public string reporttime;
}
[Serializable]
/// <summary>
/// 预报天气数据
/// </summary>
public class WeatherForecast
{
/// <summary>
/// 省份名称
/// </summary>
public string province;
/// <summary>
/// 城市名称
/// </summary>
public string city;
/// <summary>
/// 城市编码
/// </summary>
public int adcode;
/// <summary>
/// 预报发布时间
/// </summary>
public string reporttime;
/// <summary>
/// 预报数据列表
/// </summary>
public CastInfo[] casts;
}
[Serializable]
public class CastInfo
{
/// <summary>
/// 日期
/// </summary>
public string date;
/// <summary>
/// 星期几
/// </summary>
public int week;
/// <summary>
/// 白天天气现象
/// </summary>
public string dayweather;
/// <summary>
/// 晚上天气现象
/// </summary>
public string nightweather;
/// <summary>
/// 白天温度
/// </summary>
public int daytemp;
/// <summary>
/// 晚上温度
/// </summary>
public int nighttemp;
/// <summary>
/// 白天风向
/// </summary>
public string daywind;
/// <summary>
/// 晚上风向
/// </summary>
public string nightwind;
/// <summary>
/// 白天风力
/// </summary>
public int daypower;
/// <summary>
/// 晚上风力
/// </summary>
public int nightpower;
}
? ? ? ? ?使用Unity内置序列化/反序列化工具类JsonUtility将数据反序列化:
Weather.Instance.Get("320115", Weather.GetDataType.Forecast, data =>
{
WeatherResponse response = JsonUtility.FromJson<WeatherResponse>(data);
//TODO
});
??欢迎关注公众号 “当代野生程序猿”
|