1.中国天气网城市代码
TXT百度分享链接
提取码:98ip
北京,101010100
海淀,101010200
朝阳,101010300
顺义,101010400
…太长了自己去下载TXT找吧。。
2.Unity 获取当前城市天气
功能非原创,但是做了代码修改
炉石传说一套牌30张改一张就是原创- 不是 -
主要是我找不到之前看的地址了2333
有知道的可以评论回答我,我加上转载地址
存储天气数据的类声明
//类 - 获取天气namespace 命名空间你自己随意吧{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; }}}
获取数据的函数
// 获取当前天气private resp GetWeather(Int32 nint)//101210612{//获取中国天气网的string weatherInfoUrl = "/WeatherApi?citykey=" + nint; //后面数字是城市代码string weatherstr = getHtml2(weatherInfoUrl);Debug.Log(weatherstr);//这里是全部数据 xml//resp tempInfo = XmlDeSeralizer<resp>(weatherstr);return XmlDeSeralizer<resp>(weatherstr);}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;}}
Debug.Log完整数据示例
实际上是没有换行的,这些换行是为了让你们看着清楚我自己在这一个个换的。。。
不过这不影响用XML读取数据
<?xml version="1.0" encoding="UTF-8"?><resp><city>黄岩</city><updatetime>09:31</updatetime><wendu>14</wendu><fengli><![CDATA[2级]]></fengli><shidu>82%</shidu><fengxiang>南风</fengxiang><sunrise_1>06:09</sunrise_1><sunset_1>18:01</sunset_1><sunrise_2></sunrise_2><sunset_2></sunset_2><yesterday><date_1>9日星期三</date_1><high_1>高温 21℃</high_1><low_1>低温 6℃</low_1><day_1><type_1>晴</type_1><fx_1>南风</fx_1><fl_1><![CDATA[1级]]></fl_1></day_1><night_1><type_1>多云</type_1><fx_1>南风</fx_1><fl_1><![CDATA[1级]]></fl_1></night_1></yesterday><forecast><weather><date>10日星期四</date><high>高温 22℃</high><low>低温 10℃</low><day><type>多云</type><fengxiang>南风</fengxiang><fengli><![CDATA[2级]]></fengli></day><night><type>多云</type><fengxiang>南风</fengxiang><fengli><![CDATA[2级]]></fengli></night></weather><weather><date>11日星期五</date><high>高温 23℃</high><low>低温 13℃</low><day><type>晴</type><fengxiang>东南风</fengxiang><fengli><![CDATA[1级]]></fengli></day><night><type>晴</type><fengxiang>东南风</fengxiang><fengli><![CDATA[1级]]></fengli></night></weather><weather><date>12日星期六</date><high>高温 22℃</high><low>低温 14℃</low><day><type>小雨</type><fengxiang>南风</fengxiang><fengli><![CDATA[2级]]></fengli></day><night><type>多云</type><fengxiang>南风</fengxiang><fengli><![CDATA[2级]]></fengli></night></weather><weather><date>13日星期天</date><high>高温 27℃</high><low>低温 14℃</low><day><type>小雨</type><fengxiang>南风</fengxiang><fengli><![CDATA[1级]]></fengli></day><night><type>阴</type><fengxiang>南风</fengxiang><fengli><![CDATA[1级]]></fengli></night></weather><weather><date>14日星期一</date><high>高温 26℃</high><low>低温 14℃</low><day><type>多云</type><fengxiang>东南风</fengxiang><fengli><![CDATA[1级]]></fengli></day><night><type>多云</type><fengxiang>东南风</fengxiang><fengli><![CDATA[1级]]></fengli></night></weather></forecast><zhishus><zhishu><name>穿衣指数</name><value>薄款春秋装</value><detail>天气舒适,温度适宜。建议着T恤衫、休闲服等,既舒适,又时尚。</detail></zhishu><zhishu><name>紫外线强度</name><value>紫外线很强</value><detail>紫外线较强,外出要涂防晒霜,戴遮阳帽。上午10点至下午4点尽量避免阳光直射。紫外线过敏人群外出注意防护。</detail></zhishu><zhishu><name>护肤指数</name><value>清洁护肤</value><detail>空气湿润度高,可以根据皮肤类型选择相应清爽型扶肤品。</detail></zhishu><zhishu><name>洗车指数</name><value>很适宜洗车</value><detail>天气晴朗,放心洗车吧,今明两天都暂无降水~</detail></zhishu><zhishu><name>感冒指数</name><value>易发感冒</value><detail>感冒易发期,外出请适当调整衣物,注意补充水分。</detail></zhishu><zhishu><name>晾晒指数</name><value>较适宜晾晒</value><detail>预计白天没有降水,温度适宜,较适合晾晒.</detail></zhishu><zhishu><name>户外指数</name><value>较适宜外出</value><detail>天气还可以,预计白天没有降水,适合参加户外活动,适当锻炼身体。</detail></zhishu><zhishu><name>污染指数</name><value>轻微污染</value><detail>空气质量良好,污染物浓度低,对健康人群无明显影响,可在户外适当活动。</detail></zhishu><zhishu><name>钓鱼指数</name><value>适宜</value><detail>这样的天气非常适宜外出钓鱼,约个钓友出去钓鱼吧。</detail></zhishu><zhishu><name>中暑指数</name><value>不易中暑</value><detail>预计今日整体天气适中,中暑的可能性较低,无需做特殊防护。</detail></zhishu><zhishu><name>舒适度</name><value>较舒适</value><detail>天气凉爽,微微冷,对于大部分人来说是舒适的。</detail></zhishu><zhishu><name>赏月指数</name><value>较适宜赏月</value><detail>今晚条件尚可,可以赏月</detail></zhishu></zhishus></resp><!-- 10.65.5.189(10.65.5.189):36810 ; 10.65.0.222:8080 -->