微信公众平台使用百度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);
        }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值