天气预报的web服务的使用

本文介绍了一个使用C#调用Web API获取天气信息的例子。通过添加Web引用并实例化天气服务,实现了获取指定城市的天气数据,并展示如何解析返回的数据来更新UI元素。

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

重命名为:weather

 public string[] GetWeather(string city)
        {
            weather.WeatherWebService webweather = new cheshi.weather.WeatherWebService();//实例化天气的服务
            string[] wcity = webweather.getWeatherbyCityName(city);
            return wcity;
        }

        private void weathers_Shown(object sender, EventArgs e)
        {
            try
            {
                weather.WeatherWebService wweather = new cheshi.weather.WeatherWebService();
                string[] area = wweather.getSupportProvince();
                int mount = area.Length - 1;
                comboBox1.Items.Clear();
                for (int im = 0; im < mount; im++)
                {
                    comboBox1.Items.Add(area[im].ToString());

                }
                comboBox1.SelectedIndex = 0;
            }
            catch { }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                weather.WeatherWebService weweather = new cheshi.weather.WeatherWebService();
                string[] mcity = weweather.getSupportCity(comboBox1.Text);
                int mcount = mcity.Length - 1;
                comboBox2.Items.Clear();
                for (int i = 0; i < mcount; i++)
                {
                    comboBox2.Items.Add(mcity[i].Remove(mcity[i].IndexOf("(")));
                }
                comboBox2.SelectedIndex = 0;
            }
            catch { }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string[] cityweather = GetWeather(comboBox2.Items[comboBox2.SelectedIndex].ToString());
                label1.Text = cityweather[0].ToString();
                label2.Text = cityweather[1].ToString();
                label3.Text = cityweather[10].ToString();
                //今天
                pictureBox1.ImageLocation =@"images/weather/" +cityweather[8].ToString();
                pictureBox2.ImageLocation = @"images/weather/" + cityweather[9].ToString();
                label4.Text = cityweather[6].ToString() + cityweather[5].ToString() + cityweather[7].ToString();
                label5.Text = cityweather[11].ToString();
                //明天
                label6.Text = cityweather[13].ToString() + cityweather[12].ToString() + cityweather[14].ToString();
                //后天
                label7.Text = cityweather[18].ToString() + cityweather[17].ToString() + cityweather[19].ToString();
                //城市说明及图片
                pictureBox3.ImageLocation = @"http://www.cma.gov.cn/tqyb/img/city/" + cityweather[3].ToString();
                label8.Text = cityweather[22].ToString();
                //预计时间
                label9.Text = "预报时间:" + cityweather[4].ToString();
            }
            catch { }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值