获取天气服务

第一种方法

1.添加引用:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

2.cs代码:

View Code
WeatherWebServiceSoap w = new WeatherWebServiceSoapClient("WeatherWebServiceSoap");
         protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
             {
                 BindPro();
                 BindCity();
             }
         }
         //province
         protected void BindPro()
         {
             string[] pro = w.getSupportProvince();
             for (int i = 0; i < pro.Length; i++)
             {
                 ddlProv.Items.Add(new ListItem(pro[i],pro[i]));
             }
         }
         //city
         protected void BindCity()
         {
             ddlCity.Items.Clear();
             string[] city = w.getSupportCity(ddlProv.SelectedValue);
             for (int i = 0; i < city.Length; i++)
             {
                 ddlCity.Items.Add(new ListItem(city[i],city[i]));
             }
         }
         //weather
         protected void BindWeather()
         {
             string city = ddlCity.SelectedValue.ToString().Substring(0,2);
             //Response.Write(city);
             string[] mystr = w.getWeatherbyCityName(city);
             //for (int i = 0; i < mystr.Length; i++)
             //{
             //  Response.Write(mystr[8]+"<br/>");
               //img.ImageUrl = "images/a_" + mystr[8];
             //}
               //mystr[0] 省
               //mystr[2] 城市后的代码
             
            labCity.Text = mystr[1];//城市
             Label2.Text = mystr[6];//日期+天气情况(如:7月4日 多云)
             Label3.Text = mystr[5];//温度(如:28℃/34℃)
             Label4.Text = mystr[4];//时间(如:2012-7-4 12:04:38)
         }
 
        protected void ddlProv_SelectedIndexChanged(object sender, EventArgs e)
         {
             BindCity();
         }
 
        protected void Button1_Click(object sender, EventArgs e)
         {
             BindWeather();
         }
 
 
 
html代码:
 
<body>
     <form id="form1" runat="server">
     <div>
         <asp:DropDownList ID="ddlProv" runat="server" AutoPostBack="True" onselectedindexchanged="ddlProv_SelectedIndexChanged"></asp:DropDownList>
         <asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>
         <asp:Label ID="labCity" runat="server" Text="Label"></asp:Label>
         <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
         <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
         <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
         <br />
         <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="获 取" />
     </div>
     </form>
 </body>

第二种方法
1.添加引用:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

2.cs代码:

 //获取天气
             Weather.WeatherWebServiceSoapClient w = new Weather.WeatherWebServiceSoapClient("WeatherWebServiceSoap"); 
                //把webservice当做一个类来操作  
            string[] s = new string[23];
            //声明string数组存放返回结果 
            string city = "北京";
            //获得查询城市    
            s = w.getWeatherbyCityName(city); 
            //以文本框内容为变量实现方法getWeatherbyCityName   
            if (s[8] == "")  
            {
                //    MessageBox.Show("暂时不支持您查询的城市");  
            } else 
            {  
                  //pictureBox1.Image = Image.FromFile(@"d:\image\" + s[8] + ""); 
                
                string st1 =s[1]+" "+s[6]; 
                string str2 = s[10];     
                }  

  

posted @ 2013-03-28 14:57  银河系上的地球  阅读(324)  评论(0编辑  收藏  举报