WebService实现天气预报
什么是webService
WebService是基于Web的服务。它使用Web(HTTP)方式,接收和响应外部系统的某种请求,从而实现远程调用。
WebService的协议和语言
WebService所使用的数据是基于XML格式的。WebService在数据格式上主要采用SOAP协议。
SOAP即简单对象访问协议(Simple Object Access Protocal),它是用于交换XML编码信息的轻量级协议。
Web Service描述语言WSDL是用于描述Web Service及其函数、参数和返回值。
WebService实现天气查询
1. 首先拿到Web服务
2.查看功能函数
getSupportCity:通过给定省份名称得到全部支持的城市
getSupportDataSet:获得一个dataset集合,包括所有支持的省份城市
getSupportProvince:获得国内所有支持的省份
getWeatherbyCityName :获得指定城市的天气信息
3.新建项目
打开VS-新建一个Web项目
添加服务引用-高级-添加Web引用
新建一个Web页面
前端内容:
<form id="form1" runat="server">
<div>
<label>省份:</label><asp:DropDownList ID="Province" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Province_SelectedIndexChanged"></asp:DropDownList><br/>
<br/>
<label>城市:</label><asp:DropDownList ID="City" runat="server" AutoPostBack="True"></asp:DropDownList><br/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
<br/>
<div>
<label>查询日期:</label><asp:Label ID="Label7" runat="server" Text="Label"></asp:Label><br/>
<label>查询城市:</label><asp:Label ID="Label8" runat="server" Text="Label"></asp:Label><br/>
<label>今日实况:</label><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br/>
<label>今日天气:</label><asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Image ID="Image1" runat="server" />
<asp:Image ID="Image2" runat="server" /><br/>
<label>今日指数:</label><asp:Label ID="Label5" runat="server" Text="Label"></asp:Label><br/>
<label>明日天气:</label><asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<asp:Image ID="Image3" runat="server" />
<asp:Image ID="Image4" runat="server" /><br/>
<label>后日天气:</label><asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
<asp:Image ID="Image5" runat="server" />
<asp:Image ID="Image6" runat="server" /><br/>
<label>城市简介:</label><br/>
<asp:Image ID="Image7" runat="server" />
<asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
</div>
</form>
后端内容:
WeatherWebService client = new WeatherWebService();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindProvince();
}
}
//省份数据的绑定
public void BindProvince()
{
DataSet ds = client.getSupportDataSet();
Province.DataSource = ds.Tables[0].DefaultView;
Province.DataTextField = "Zone";
Province.DataValueField = "ID";
Province.DataBind();
}
//城市数据的绑定
public void BindCity(string zongID)
{
DataSet ds = client.getSupportDataSet();
DataView dv = new DataView(ds.Tables[1]);
dv.RowFilter = "ZoneID=" + zongID;
City.DataSource = dv;
City.DataTextField = "Area";
City.DataValueField = "AreaCode";
City.DataBind();
}
//天气数据的绑定
public void BindCityWeather(string cityName)
{
string[] weather = client.getWeatherbyCityName(cityName);
Label1.Text = weather [10];
Label2.Text = weather [6] + " " + weather [5] + " " + weather [7];
Label3.Text = weather [13] + " " + weather [12] + " " + weather [14];
Label4.Text = weather [18] + " " + weather [17] + " " + weather [19];
Label5.Text = weather [11];
Label6.Text = weather [22];
Label7.Text = Convert.ToDateTime(weather [4]).ToLongDateString();
Label8.Text = weather [0] + " / " + weather [1];
Image1.ImageUrl = "~/Resource/weather/" + weather [8];
Image2.ImageUrl = "~/Resource/weather/" + weather [9];
Image3.ImageUrl = "~/Resource/weather/" + weather [15];
Image4.ImageUrl = "~/Resource/weather/" + weather [16];
Image5.ImageUrl = "~/Resource/weather/" + weather [20];
Image6.ImageUrl = "~/Resource/weather/" + weather [21];
Image7.ImageUrl = "~/Resource/City2/" + weather [3];
Image7.AlternateText = City.SelectedItem.Text;
}
//按钮点击事件
protected void Button1_Click(object sender, EventArgs e)
{
//Button控件的点击事件会触发IsPostBack
string cityName = City.SelectedItem.Value.ToString();
BindCityWeather(cityName);
}
//省份下拉列表的选择改变事件
protected void Province_SelectedIndexChanged(object sender, EventArgs e)
{
//DropDownList的SelectedIndexChanged无法触发解决办法
//把控件的AutoPostTBack设为True,在IsPostBack中写处理方法
string ID = Province.SelectedItem.Value.ToString();
BindCity(ID);
}
保存-运行。