目前还有效的 天气接口:
第一种(使用BastHTTP2插件请求):
返回数据编码格式为GZIP的json或者xml,untiy自带的 UnityWebRequest网络请求方法 解析不出来, 需要使用插件BestHTTP2请求 才可以,具体原因 可能是插件内部转化了数据。 BastHTTP2下载地址:BastHTTP2下载 接口链接: 城市名字获取天气 json数据: http://wthrcdn.etouch.cn/weather_mini?city=北京 通过城市id获得天气数据,json数据 http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100 通过城市id获得天气数据,xml文件数据 http://wthrcdn.etouch.cn/WeatherApi?city=北京 代码实例:
void ReqStart()
{
string url = "http://wthrcdn.etouch.cn/weather_mini?city=苏州";
new HTTPRequest(new Uri(url), (req, response) => {
if (response!=null)
{
string text = response.DataAsText;
Debug.LogFormat("苏州天气返回数据:{0}",text);
}
}).Send();
}
返回数据:
第二种 UnityWebRequest请求 不使用任何插件
- 国家气象局:
实时天气1(没有天气数据 只有温度风力等一些数据):http://www.weather.com.cn/data/sk/101190401.html 实时天气2(有天气 但不准 应该 此接口 停止维护了):http://www.weather.com.cn/data/cityinfo/101190401.html 第三方接口(webgl 不能使用 报跨域问题 ): http://t.weather.itboy.net/api/weather/city/101190401 第三方接口2(返回JSON数据 webgl 可直接使用) https://api.help.bj.cn/apis/weather/?id=101190401 代码实例:
IEnumerator GetWeather()
{
string url = "https://api.help.bj.cn/apis/weather/?id=101190401";
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
Debug.Log("请求错误");
}
else
{
string str = request.downloadHandler.text;
Debug.LogFormat("苏州天气返回数据:{0}", str);
}
}
返回数据展示:
|