经过查找,找到一个免费的天气接口api ,现在提供给大家使用。
天气接口api 地址:/weather_mini?city=城市名称
调用实例:输入参数Url为/weather_mini?city=
输入参数postDataStr为城市名称 例如城市名称=北京
调用代码如下:
1 public string HttpGet(string Url, string postDataStr) 2 { 3 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr); 4 request.Method = "Get"; 5 request.ContentType = "text/html;charset=UTF-8"; 6 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 7 Stream myResponseStream = response.GetResponseStream(); 8 var ce = response.ContentEncoding; 9 if (ce.ToLower() == "gzip")10 {11 myResponseStream = new GZipStream(myResponseStream,CompressionMode.Decompress);12 }13 StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);14 string retString = myStreamReader.ReadToEnd();15 myStreamReader.Close();16 myResponseStream.Close();17 return retString;18 }
View Code
返回结果如下:
{"data":{"yesterday":{"date":"24日星期三","high":"高温 33℃","fx":"西南风","low":"低温 17℃","fl":"3-4级","type":"多云"},"city":"北京","aqi":"43","forecast":[{"date":"25日星期四","high":"高温 30℃","fengli":"3-4级","low":"低温 16℃","fengxiang":"南风","type":"晴"},{"date":"26日星期五","high":"高温 31℃","fengli":"微风级","low":"低温 17℃","fengxiang":"南风","type":"晴"},{"date":"27日星期六","high":"高温 35℃","fengli":"微风级","low":"低温 21℃","fengxiang":"西南风","type":"晴"},{"date":"28日星期天","high":"高温 34℃","fengli":"3-4级","low":"低温 21℃","fengxiang":"北风","type":"多云"},{"date":"29日星期一","high":"高温 31℃","fengli":"微风级","low":"低温 18℃","fengxiang":"南风","type":"多云"}],"ganmao":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。","wendu":"27"},"status":1000,"desc":"OK"}
自己根据需要处理一下返回结果。