本文通過一個完整的實例,講解如何通過訪問中國天氣網提供的API介面,實現天氣預報功能。 實現天氣預報功能的相關知識點: 天氣預報介面【提供風向,風速等功能】:http://www.weather.com.cn/data/sk/101120201.html 天氣預報介面【提供天氣基礎功能】:http: ...
本文通過一個完整的實例,講解如何通過訪問中國天氣網提供的API介面,實現天氣預報功能。
實現天氣預報功能的相關知識點:
- 天氣預報介面【提供風向,風速等功能】:http://www.weather.com.cn/data/sk/101120201.html
- 天氣預報介面【提供天氣基礎功能】:http://www.weather.com.cn/data/cityinfo/101120201.html
- 新浪網的介面【其中city後的是城市的名稱轉碼】http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0
- HttpWebRequest/HttpWebResponse 訪問網路的相關資源
- XmlSerializer 中序列化與反序列化功能
- Newtonsoft.Json中Json字元串的反序列化功能
- 泛型,委托
具體如圖所示:
天氣代碼,對接天氣API介面返回的實體類,兩個介面對應兩個實體類:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using Newtonsoft.Json; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Xml.Serialization; 8 9 namespace DemoWeather 10 { 11 /// <summary> 12 /// 城市基礎天氣信息類 13 /// </summary> 14 [Serializable] 15 public class CWeather 16 { 17 [JsonProperty("weatherinfo")] 18 public CWeatherInfo WeatherInfo { get; set; } 19 } 20 21 [Serializable] 22 public class CWeatherInfo 23 { 24 [JsonProperty("city")] 25 public string City { get; set; } 26 27 [JsonProperty("cityid")] 28 public string CityId { get; set; } 29 30 /// <summary> 31 /// 氣溫1 32 /// </summary> 33 [JsonProperty("temp1")] 34 public string Temp1 { get; set; } 35 36 /// <summary> 37 /// 氣溫2 38 /// </summary> 39 [JsonProperty("temp2")] 40 public string Temp2 { get; set; } 41 42 /// <summary> 43 /// 天氣情況 44 /// </summary> 45 [JsonProperty("weather")] 46 public string Weather { get; set; } 47 48 [JsonProperty("img1")] 49 public string Img1 { get; set; } 50 51 [JsonProperty("img2")] 52 public string Img2 { get; set; } 53 54 /// <summary> 55 /// 更新時間 56 /// </summary> 57 [JsonProperty("ptime")] 58 public string PTime { get; set; } 59 60 } 61 62 /// <summary> 63 /// 城市基礎天氣信息類(另外一個) 64 /// </summary> 65 [Serializable] 66 public class DWeather 67 { 68 [JsonProperty("weatherinfo")] 69 public DWeatherInfo WeatherInfo { get; set; } 70 } 71 72 [Serializable] 73 public class DWeatherInfo { 74 [JsonProperty("city")] 75 public string City { get; set; } 76 77 [JsonProperty("cityid")] 78 public string CityId { get; set; } 79 80 /// <summary> 81 /// 氣溫1 82 /// </summary> 83 [JsonProperty("temp")] 84 public string Temp { get; set; } 85 86 /// <summary> 87 /// 風向 88 /// </summary> 89 [JsonProperty("WD")] 90 public string WD { get; set; } 91 92 /// <summary> 93 /// 風速 94 /// </summary> 95 [JsonProperty("WS")] 96 public string WS { get; set; } 97 98 /// <summary> 99 /// 相對濕度 100 /// </summary> 101 [JsonProperty("SD")] 102 public string SD { get; set; } 103 104 /// <summary> 105 /// 風力 106 /// </summary> 107 [JsonProperty("WSE")] 108 public string WSE { get; set; } 109 110 /// <summary> 111 /// 更新時間 112 /// </summary> 113 [JsonProperty("time")] 114 public string Time { get; set; } 115 116 /// <summary> 117 /// 是否有雷達圖(1表示有雷達圖) 118 /// </summary> 119 [JsonProperty("isRadar")] 120 public string IsRadar { get; set; } 121 122 /// <summary> 123 /// 雷達圖地址(AZ9532為青島雷達) 124 /// </summary> 125 [JsonProperty("Radar")] 126 public string Radar { get; set; } 127 128 /// <summary> 129 /// 130 /// </summary> 131 [JsonProperty("njd")] 132 public string Njd { get; set; } 133 134 [JsonProperty("qy")] 135 public string Qy { get; set; } 136 137 [JsonProperty("rain")] 138 public string Rain { get; set; } 139 140 } 141 142 /// <summary> 143 /// 新浪天氣預報API實體類 144 /// </summary> 145 [Serializable] 146 [XmlRoot("Profiles")] 147 public class SWeather { 148 149 [JsonProperty("Weather")] 150 [XmlElementAttribute("Weather")] 151 public SWeatherInfo WeatherInfo { get; set; } 152 } 153 154 public class SWeatherInfo { 155 156 /// <summary> 157 /// 對應的查詢城市 158 /// </summary> 159 [JsonProperty("city")] 160 [XmlElementAttribute("city")] 161 public string City { get; set; } 162 163 /// <summary> 164 /// 白天天氣情況 165 /// </summary> 166 [JsonProperty("status1")] 167 [XmlElementAttribute("status1")] 168 public string Status1 { get; set; } 169 170 /// <summary> 171 /// 夜間天氣情況 172 /// </summary> 173 [JsonProperty("status2")] 174 [XmlElementAttribute("status2")] 175 public string Status2 { get; set; } 176 177 /// <summary> 178 /// 白天天氣情況拼音 179 /// </summary> 180 [JsonProperty("figure1")] 181 [XmlElementAttribute("figure1")] 182 public string Figure1 { get; set; } 183 184 /// <summary> 185 /// 夜間天氣情況拼音 186 /// </summary> 187 [JsonProperty("figure2")] 188 [XmlElementAttribute("figure2")] 189 public string Figure2 { get; set; } 190 191 /// <summary> 192 /// 白天風向 193 /// </summary> 194 [JsonProperty("direction1")] 195 [XmlElementAttribute("direction1")] 196 public string Direction1 { get; set; } 197 198 /// <summary> 199 /// 夜晚風向 200 /// </summary> 201 [JsonProperty("direction2")] 202 [XmlElementAttribute("direction2")] 203 public string Direction2 { get; set; } 204 205 /// <summary> 206 /// 白天風力 207 /// </summary> 208 [JsonProperty("power1")] 209 [XmlElementAttribute("power1")] 210 public string Power1 { get; set; } 211 212 /// <summary> 213 /// 夜間風力 214 /// </summary> 215 [JsonProperty("power2")] 216 [XmlElementAttribute("power2")] 217 public string Power2 { get; set; } 218 219 /// <summary> 220 /// 白天溫度 221 /// </summary> 222 [JsonProperty("temperature1")] 223 [XmlElementAttribute("temperature1")] 224 public string Temperature1 { get; set; } 225 226 /// <summary> 227 /// 夜間溫度 228 /// </summary> 229 [JsonProperty("temperature2")] 230 [XmlElementAttribute("temperature2")] 231 public string Temperature2 { get; set; } 232 233 /// <summary> 234 /// 體感指數 235 /// </summary> 236 [JsonProperty("ssd")] 237 [XmlElementAttribute("ssd")] 238 public string Ssd { get; set; } 239 240 /// <summary> 241 /// 白天體感溫度 242 /// </summary> 243 [JsonProperty("tgd1")] 244 [XmlElementAttribute("tgd1")] 245 public string Tgd1 { get; set; } 246 247 /// <summary> 248 /// 夜間體感溫度 249 /// </summary> 250 [JsonProperty("tgd2")] 251 [XmlElementAttribute("tgd2")] 252 public string Tgd2 { get; set; } 253 254 /// <summary> 255 /// 紫外線強度 256 /// </summary> 257 [JsonProperty("zwx")] 258 [XmlElementAttribute("zwx")] 259 public string Zwx { get; set; } 260 261 /// <summary> 262 /// 空調指數 263 /// </summary> 264 [JsonProperty("ktk")] 265 [XmlElementAttribute("ktk")] 266 public string Ktk { get; set; } 267 268 /// <summary> 269 /// 污染指數 270 /// </summary> 271 [JsonProperty("pollution")] 272 [XmlElementAttribute("pollution")] 273 public string Pollution { get; set; } 274 275 /// <summary> 276 /// 洗車指數 277 /// </summary> 278 [JsonProperty("xcz")] 279 [XmlElementAttribute("xcz")] 280 public string Xcz { get; set; } 281 282 /// <summary> 283 /// 綜合指數這個我不確定 284 /// </summary> 285 [JsonProperty("zho")] 286 [XmlElementAttribute("zho")] 287 public string Zho { get; set; } 288 289 /// <summary> 290 /// 沒猜出來是什麼指數,沒有數值 291 /// </summary> 292 [JsonProperty("diy")] 293 [XmlElementAttribute("diy")] 294 public string Diy { get; set; } 295 296 /// <summary> 297 /// 同上 298 /// </summary> 299 [JsonProperty("fas")] 300 [XmlElementAttribute("fas")] 301 public string Fas { get; set; } 302 303 /// <summary> 304 /// 穿衣指數 305 /// </summary> 306 [JsonProperty("chy")] 307 [XmlElementAttribute("chy")] 308 public string Chy { get; set; } 309 310 /// <summary> 311 /// zho的說明,然而zho是什麼指數我也不確定 312 /// </summary> 313 [JsonProperty("zho_shuoming")] 314 [XmlElementAttribute("zho_shuoming")] 315 public string Zho_shuoming { get; set; } 316 317 /// <summary> 318 /// 同上 319 /// </summary> 320 [JsonProperty("diy_shuoming")] 321 [XmlElementAttribute("diy_shuoming")] 322 public string Diy_shuoming { get; set; } 323 324 /// <summary> 325 /// 同上 326 /// </summary> 327 [JsonProperty("fas_shuoming")] 328 [XmlElementAttribute("fas_shuoming")] 329 public string Fas_shuoming { get; set; } 330 331 /// <summary> 332 /// 穿衣指數說明 333 /// </summary> 334 [JsonProperty("chy_shuoming")] 335 [XmlElementAttribute("chy_shuoming")] 336 public string Chy_shuoming { get; set; } 337 338 /// <summary> 339 /// 污染程度 340 /// </summary> 341 [JsonProperty("pollution_l")] 342 [XmlElementAttribute("pollution_l")] 343 public string Pollution_l { get; set; } 344 345 /// <summary> 346 /// 紫外線指數概述 347 /// </summary> 348 [JsonProperty("zwx_l")] 349 [XmlElementAttribute("zwx_l")] 350 public string Zwx_l { get; set; } 351 352 /// <summary> 353 /// 體感指數概述 354 /// </summary> 355 [JsonProperty("ssd_l")] 356 [XmlElementAttribute("ssd_l")] 357 public string Ssd_l { get; set; } 358 359 /// <summary> 360 /// 這個不知道 361 /// </summary> 362 [JsonProperty("fas_l")] 363 [XmlElementAttribute("fas_l")] 364 public string Fas_l { get; set; } 365 366 /// <summary> 367 /// 這個不知道 368 /// </summary> 369 [JsonProperty("zho_l")] 370 [XmlElementAttribute("zho_l")] 371 public string Zho_l { get; set; } 372 373 /// <summary> 374 /// 穿衣指數概述(可理解為穿衣建議) 375 /// </summary> 376 [JsonProperty("chy_l")] 377 [XmlElementAttribute("chy_l")] 378 public string Chy_l { get; set; } 379 380 /// <summary> 381 /// 空調指數概述 382 /// </summary> 383 [JsonProperty("ktk_l")] 384 [XmlElementAttribute("ktk_l")] 385 public string Ktk_l { get; set; } 386 387 /// <summary> 388 /// 洗車指數概述 389 /// </summary> 390 [JsonProperty("xcz_l")] 391 [XmlElementAttribute("xcz_l")] 392 public string Xcz_l { get; set; } 393 394 /// <summary> 395 /// 這個不知道 396 /// </summary> 397 [JsonProperty("diy_l")] 398 [XmlElementAttribute("diy_l")] 399 public string Diy_l {