获取实时天气 =。=
/***********************
* Title: 检测当天实时天气
* FileName:
* Date: 2020
* Author: 玄策
* Version: 1.0
* UnityVersion: 2019.2.4f1
* Description:
* Func:
* -
***********************/
using Soian.Zhitai.Models.WeatherForecast;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
using System.Xml.Serialization;
using UnityEngine;
//类
namespace Soian.Zhitai.Models.WeatherForecast
{
public class resp
{
public string city { get; set; }
public string updatetime { get; set; }
public string wendu { get; set; }
public string fengli { get; set; }
public string shidu { get; set; }
public string fengxiang { get; set; }
public environment environment { get; set; }
public alarm alarm { get; set; }
public List<weather> forecast { set; get; }
}
public class environment
{
public string aqi { get; set; }
public string pm25 { get; set; }
public string suggest { get; set; }
public string quality { get; set; }
public string MajorPollutants { get; set; }
public string time { get; set; }
}
public class alarm
{
public string cityName { get; set; }
public string alarmType { get; set; }
public string alarmDegree { get; set; }
public string alarmText { get; set; }
public string alarm_details { get; set; }
public string standard { get; set; }
public string suggest { get; set; }
}
public class weather
{
public string date { get; set; }
public string high { get; set; }
public string low { get; set; }
public climate day { get; set; }
public climate night { get; set; }
}
public class climate
{
public string type { get; set; }
public string fengxiang { get; set; }
public string fengli { get; set; }
}
}
public class weather : MonoBehaviour
{
void Start()
{
//获取中国天气网的
string weatherInfoUrl = "http://wthrcdn.etouch.cn/WeatherApi?citykey=" + 101060101; //后面数字是城市代码(网上查)
string weatherstr = getHtml2(weatherInfoUrl);
Debug.Log(weatherstr);
resp tempInfo = XmlDeSeralizer<resp>(weatherstr);
string str_wea = tempInfo.forecast[0].day.type;
if (str_wea.Contains("雨"))
{
Debug.Log("雨天");
ChangeSky.Instance.Tog_Rain.isOn = true;
}
else if (str_wea.Contains("雪")
|| str_wea.Contains("雹"))
{
Debug.Log("雪天");
ChangeSky.Instance.Tog_Snow.isOn = true;
}
else
{
Debug.Log("晴天");
ChangeSky.Instance.Tog_Sunny.isOn = true;
}
}
private static string getHtml2(string url)
{
StringBuilder s = new StringBuilder(102400);
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
HttpWebResponse response = (HttpWebResponse)wr.GetResponse(); head(response);
GZipStream g = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
byte[] d = new byte[20480];
int l = g.Read(d, 0, 20480);
while (l > 0)
{
s.Append(Encoding.UTF8.GetString(d, 0, l));
l = g.Read(d, 0, 20480);
}
return s.ToString();
}
private static void head(HttpWebResponse r)
{
string[] keys = r.Headers.AllKeys; for (int i = 0; i < keys.Length; ++i)
{
Console.WriteLine(keys[i] + " " + r.Headers[keys[i]]);
}
}
public static T XmlDeSeralizer<T>(string xmlStr) where T : class, new()
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(xmlStr))
{
return xs.Deserialize(reader) as T;
}
}
}
网友评论