[整]C#获取天气预报信息(baidu api)包括pm2.5

 1 /// <summary>
 2         /// 获取天气预报信息
 3         /// </summary>
 4         /// <returns></returns>
 5         public BaiduTQ GetWeather()
 6         {
 7             // GetCity()获得的信息解析后,填充丰台部分
 8             string url = @"http://api.map.baidu.com/telematics/v3/weather?location=丰台&output=json&ak=hXWAgbsCC9UTkBO5V5Qg1WZ9";
 9 
10             HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;
11             webRequest.Method = "GET";
12             webRequest.ContentType = "text/html";
13             
14             using(StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream()))
15             {
16                string str =  sr.ReadToEnd();
17                BaiduTQ b = JsonConvert.DeserializeObject<BaiduTQ>(str);
18 
19                return b;
20             }
21         }
获取天气预报信息
 1 public class BaiduTQ
 2     {
 3         public int error { get; set; }
 4         public string status { get; set; }
 5         public string date { get; set; }
 6         public List<BaiduResult> results { get; set; }
 7     }
 8  
 9     public class BaiduResult
10     {
11         public string currentCity { get; set; }
12         public string pm25 { get; set; }
13         public List<BaiduIndex> index { get; set; }
14         public List<BaiDuWeaterData> weather_data { get; set; }
15     }
16  
17     public class BaiduIndex
18     {
19         public string title { get; set; }
20         public string zs { get; set; }
21         public string tipt { get; set; }
22         public string des { get; set; }
23     }
24  
25     public class BaiDuWeaterData
26     {
27         public string date { get; set; }
28         public string dayPictureUrl { get; set; }
29         public string nightPictureUrl { get; set; }
30         public string weather { get; set; }
31         public string wind { get; set; }
32         public string temperature { get; set; }
33     }
BaiduTQ


 

 1  /// <summary>
 2         /// 根据ip获取城市信息
 3         /// </summary>
 4         /// <returns>ip所在城市信息</returns>
 5         private string GetCity()
 6         {
 7             IPAddress ip = this.GetIPAddress();
 8             if (ip == null) return null;
 9 
10             using (var client = new WebClient())
11             {
12                 var url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip;
13                 client.Encoding = Encoding.UTF8;
14                 var str = client.DownloadString(url);
15 
16                 return str;
17             }
18         }
根据ip获取所在地
 1 /// <summary>
 2         /// 获取本地ip地址
 3         /// </summary>
 4         /// <returns></returns>
 5         private IPAddress GetIPAddress()
 6         {
 7             IPAddress[] arrIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());
 8             foreach (IPAddress ip in arrIPAddresses)
 9             {
10                 if (ip.AddressFamily.Equals(AddressFamily.InterNetwork))
11                 {
12                     return ip;
13                 }
14             }
15 
16             return null;
17         }
获取本地ip

ps:若需要获取外网ip,请参见http://www.cnblogs.com/hehexiaoxia/p/4996624.html

附录:国家气象局提供的天气预报接口及使用说明

转载于:https://www.cnblogs.com/hehexiaoxia/p/4996668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值