前言:獲取天氣接口數據,需要用一些知識點,比如解析xml,Json數據轉換成我們需要的對象,由於之前沒有在項目用過。有做成一個小Demo分享下,同時也是讓自己學習下。相關接口介紹可以參考課后網的謝周兵的文章:中國天氣網接口返回json格式分析及接口(XML、圖片接口)說明!
首先,我們要做的是確定要取數據的城市,當然可以從全國開始,本文的示例僅限在福建省內,根據城市的中文拼音獲取相關城市的天氣數據。可以創建一個天氣處理類WeatherHandler.cs。去中國天氣網獲取福建的市xml文件URL地址:http://flash.weather.com.cn/wmaps/xml/fujian.xml,獲取返回城市的URL代碼-用於取天氣數據。方法如下:
1 /// <summary> 2 /// 獲取城市Url代碼 - 用於構建請求天氣信息的Url地址 3 /// 目前只支持福建省下級城市 4 /// </summary> 5 /// <param name="cityName">城市名稱-拼音 如:xiamen</param> 6 /// <returns></returns> 7 public static string GetCityCode(string cityName) 8 { 9 // 去中國天氣網獲取福建的市xml文件 10 string provinceUrl = "http://flash.weather.com.cn/wmaps/xml/fujian.xml"; 11 12 // 解析加載回來的xml文件 13 XmlDocument doc = new XmlDocument(); 14 doc.Load(new XmlTextReader(provinceUrl)); 15 16 // 返回城市Url代碼 - 用於取天氣數據 17 string cityCode = null; 18 19 if (doc.HasChildNodes) 20 { 21 foreach (XmlNode pNode in doc.ChildNodes) 22 { 23 if (pNode.HasChildNodes) 24 { 25 foreach (XmlNode cNode in pNode.ChildNodes) 26 { 27 if (cNode.Attributes["pyName"].Value == cityName) 28 { 29 cityCode = cNode.Attributes["url"].Value; 30 break; // 跳出循環 31 } 32 } 33 } 34 } 35 } 36 37 return cityCode; 38 }
第二,根據城市名稱(如:xiamen)返回天氣類信息,用第一步的方法獲取對應返回城市Url代碼(如:101230201),構建請求訪問天氣詳細信息的Url地址(如:http://m.weather.com.cn/data/101230201.html),可以得到從中國天氣網返回的json數據,我們根據需要返回的結果創建對應的對象WeatherJson,再通過反系列化成自定義好的對象WeatherJson,這樣就可以通過對象的方式訪問對的數據了。為了讓代碼復用性更高些,請求發送數據也獨立成一個方法。參考代碼如下:
1 /// <summary> 2 /// 發送請求返回響應的數據 3 /// </summary> 4 /// <param name="url">請求的Url地址</param> 5 /// <returns></returns> 6 public static string GetRequestData(string url) 7 { 8 // 構建一個請求 9 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 10 // 請求的方式 11 request.Method = "GET"; 12 13 // 請求的響應 14 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 15 16 // 響應的流 17 Stream responseStream = response.GetResponseStream(); 18 19 // 字符編碼 20 Encoding enc = Encoding.GetEncoding("utf-8"); 21 22 // 讀取流 23 StreamReader readResponseStream = new StreamReader(responseStream, enc); 24 25 // 請求的結果 26 string result = readResponseStream.ReadToEnd(); 27 28 // 關閉流,響應,釋放資源 29 readResponseStream.Close(); 30 response.Close(); 31 32 return result; 33 34 } 35 36 /// <summary> 37 /// 根據城市名稱返回天氣類信息 38 /// 目錄只支持福建省下級城市 39 /// </summary> 40 /// <param name="cityName">城市名稱-拼音 如:xiamen</param> 41 /// <returns></returns> 42 public static WeatherInfo GetWeatherInfo(string cityName) 43 { 44 // 填充相關的WeatherInfo數據 45 WeatherInfo wInfo = null; 46 47 try 48 { 49 // 城市Url代碼 50 string cityCode = GetCityCode(cityName); 51 52 // 訪問天氣詳細信息的Url地址 53 string weatherUrl = "http://m.weather.com.cn/data/" + cityCode + ".html"; 54 55 // 從中國天氣網返回的json數據 56 string jsonData = GetRequestData(weatherUrl); 57 58 // 根據返回的Json數據反系列化自定義好的對象 59 WeatherJson json = JsonSerializerHelper.FromJsonTo<WeatherJson>(jsonData); 60 61 // 填充相關的WeatherInfo數據 62 wInfo = json.WeatherInfo; 63 } 64 catch (Exception) 65 { 66 } 67 68 return wInfo; 69 70 }
第三,就可以調用轉換好的對象進行使用了,比如我在控制台輸出我想要的數據。
1 // 確定要取的城市名稱 - 拼音 2 string cityName = "xiamen"; 3 4 5 WeatherInfo weatherInfo = WeatherHandler.GetWeatherInfo(cityName); 6 7 Console.WriteLine("weatherInfo.City:" + weatherInfo.City); 8 Console.WriteLine("weatherInfo.CityEn:" + weatherInfo.CityEn); 9 Console.WriteLine("weatherInfo.CityId:" + weatherInfo.CityId); 10 Console.WriteLine("weatherInfo.DateY:" + weatherInfo.DateY); 11 Console.WriteLine("weatherInfo.FutureTemperature:" + weatherInfo.FutureTemperature); 12 Console.WriteLine("weatherInfo.TodayTemperature:" + weatherInfo.TodayTemperature); 13 Console.WriteLine("weatherInfo.TomorrowTemperature:" + weatherInfo.TomorrowTemperature); 14 Console.WriteLine("weatherInfo.Week:" + weatherInfo.Week); 15 16 Console.ReadLine();
效果圖片:
最后,關於Json數據轉換,我參考博客園作者“頹廢”的博文Json序列化和反序列化方法實現的,這邊代碼就不帖出來了。需要了解的同學可以到相關博客去了解!項目Demo