好贷网好贷款

微信公众平台使用百度API查询天气预报

发布时间:2016-12-5 8:41:06 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"微信公众平台使用百度API查询天气预报",主要涉及到微信公众平台使用百度API查询天气预报方面的内容,对于微信公众平台使用百度API查询天气预报感兴趣的同学可以参考一下。

先去百度申请API,后就可以根据API来查询未来3天的天气预报 private void ResponseMsg(string weixinXML) { XmlDocument doc = new XmlDocument(); doc.LoadXml(weixinXML); XmlNodeList list = doc.GetElementsByTagName("xml"); XmlNode xn = list[0]; string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText; string msgType =xn.SelectSingleNode("//MsgType").InnerText; if (msgType.Equals("location")) { //处理位置消息 LocationType lt = dx.NewLocationType(dx.Xn); Response.Write(Utils.GetTianQi(lt.Label, lt.FromUserName, lt.ToUserName));//根据位置获取天气预报 } } 下面是获取的方法 public static string GetTianQi(string locat,string UserOpenId, string DevOpenId) { locat = Utils.GetCityName(locat); if (locat == string.Empty) { return ResponseWeixin.ResponseText("您好,无法确定您的城市信息!\n您可重新尝试!", UserOpenId, DevOpenId); } string url = "http://api.map.baidu.com/telematics/v3/weather?location=" + HttpContext.Current.Server.UrlEncode(locat) + "&output=xml&ak=" + BaiduKey; WebRequest webRequest = WebRequest.Create(url); HttpWebRequest request = webRequest as HttpWebRequest; request.Method = "GET"; request.ContentType = "application/json; charset=utf-8"; request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20100101 Firefox/20.0"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string tianqiXML = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(tianqiXML); XmlNode parentNode = xmlDoc.DocumentElement; XmlNode aaa = parentNode["results"]; XmlNode rList = aaa["weather_data"]; List<news> items = new List<news>(); news item1 = new news(); item1.Title = rList.ChildNodes[0].InnerText + "," + rList.ChildNodes[3].InnerText + "," + rList.ChildNodes[4].InnerText + "," + rList.ChildNodes[5].InnerText; item1.PicUrl = rList.ChildNodes[1].InnerText; news item2 = new news(); item2.Title = rList.ChildNodes[6].InnerText + "," + rList.ChildNodes[9].InnerText + "," + rList.ChildNodes[10].InnerText + "," + rList.ChildNodes[11].InnerText; item2.PicUrl = rList.ChildNodes[7].InnerText; news item3 = new news(); item3.Title = rList.ChildNodes[12].InnerText + "," + rList.ChildNodes[15].InnerText + "," + rList.ChildNodes[16].InnerText + "," + rList.ChildNodes[17].InnerText; item3.PicUrl = rList.ChildNodes[13].InnerText; items.Add(item1); items.Add(item2); items.Add(item3); return ResponseWeixin.ResponseSubscribe(items, UserOpenId, DevOpenId); }

上一篇:Weka数据挖掘
下一篇:MFC获取文件大小

相关文章

相关评论