asp.net调用WeatherService,指定城市名称查询天气预报
网友 网络
调用天气预报功能实现的效果图如下:(你可以根据自己的需要来调整代码样式和.cs中的内容来实现自己想要的效果)
 

貌似不支持IP自动定位获取天气,而且。。只能是当天天气。。。
 

首先启动VS2005 :
(1)新建网站,将其命名为WeatherService

(2)右键程序根目录,添加Web引用,在服务地址填入:http://www.ayandy.com/Service.asmx

在Web引用名输入obj ,然后添加引用。

(3)在WeatherService.aspx页面中写入:

<div>
省份:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>城市:<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="获 取" />
</div>
<div style ="border :solid 1px #8daaf4;margin-top :5px;">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label><br />
<asp:Image ID="Image1" runat="server" />

</div>

(4)在后置代码中写入:

//添加命名空间:

using System.Web.Services;
using obj;

//实例化引用对象:如不修改web.config可 Test.com.ayandy.www.Service myobj = new Test.com.ayandy.www.Service();

obj.Service myobj = new obj.Service();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindPro();
BindCity();
BindWeather();
}
}


protected void BindPro()
{
string[] pro = myobj.getSupportProvince();
for (int i = 1; i <= Int32.Parse(pro[0]); i++)
{
DropDownList1.Items.Add(new ListItem(pro[i].ToString(), pro[i].ToString()));
}
}

protected void BindCity()
{
DropDownList2.Items.Clear();
string[] city = myobj.getSupportCity(DropDownList1.SelectedValue);
for (int i = 1; i <= Int32.Parse(city[0]); i++)
{
DropDownList2.Items.Add(new ListItem(city[i].ToString(), city[i].ToString()));
}
}

protected void BindWeather()
{
string[] mystr = myobj.getWeatherbyCityName(DropDownList2.SelectedValue, theDayFlagEnum.Today);
Label1.Text = mystr[1].ToString();
Label2.Text = mystr[2].ToString();
Label3.Text = mystr[3].ToString();
string fengxaing = mystr[4];
Label4.Text = Convert.ToString(fengxaing);

Label5.Text = mystr[5].ToString();
Image1.ImageUrl = mystr[6].ToString();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
BindCity();
}
protected void Button1_Click(object sender, EventArgs e)
{
BindWeather();
}

(5)你会发现在web.config中自动添加了下面的代码:

<appSettings>
<add key="obj.Service" value="http://www.ayandy.com/Service.asmx"/>
</appSettings>

若发现没有自动添加的话 自己手动添加即可.

如果不修改web.config也可直接实例成 Test.com.ayandy.www.Service myobj = new Test.com.ayandy.www.Service();

CIO之家 www.ciozj.com 公众号:imciow
关联的文档
也许您喜欢