在ASP.NET中如何通過WebService擷取某一個省市的天氣情況呢?
1.建立一個web項目然後右鍵單擊解決方案,選擇添加web引用,輸入一個可以提供查詢天氣的WebService,如下圖所示:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5CTBljR1YDO0EDM3AzMx8FMvwlMvwlNwETMwIzLcRnbl1GajFGd0F2LcRXZu5ibkN3YukGavw1LcpDc0RHaiojIsJye.gif)
在這裡我輸入的URL是:http://www.ayandy.com/Service.asmx 其實網上有很多提供天氣服務的WebService可以選擇,但是要注意每一個service提供的API及傳回值大都是不同的。
2.建立一個aspx頁面,命名為WebService.aspx 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebService.aspx.cs" Inherits="Study.WebService" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>WebService Weather</title>
</head>
<body>
<form id="form1" runat="server">
<div>
省份:<asp:DropDownList ID="dopProvinceList" runat="server" OnSelectedIndexChanged="dopProvinceList_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
城市:
<asp:DropDownList ID="dopCityList" runat="server">
</asp:DropDownList>
<asp:Button ID="btnSearch" runat="server" Text="查詢" OnClick="btnSearch_Click" /><br />
<asp:Image ID="imgWeather" runat="server" AlternateText="天氣圖檔"/><br />
城市:<asp:Label ID="lblCity" runat="server" Text="Label" /><br />
天氣情況:<asp:Label ID="lblWeather" runat="server" Text="Label" /><br />
溫度:<asp:Label ID="lblTemperature" runat="server" Text="Label" /><br />
風力:<asp:Label ID="lblWind" runat="server" Text="Label" /><br />
日期:<asp:Label ID="lblDate" runat="server" Text="Label" />
</div>
</form>
</body>
</html>
3.背景代碼:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//引用命名空間
using Study.com.ayandy.www;
namespace Study
{
public partial class WebService : System.Web.UI.Page
{
//執行個體化對象
Service ws = new Service();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindProvince();
this.BindCity();
//this.GetWeather();
}
}
/// <summary>
/// 綁定省份清單
/// </summary>
private void BindProvince()
{
string[] pro = ws.getSupportProvince();
for (int i = 1; i <= Int32.Parse(pro[0]); i++)
{
this.dopProvinceList.Items.Add(new ListItem(pro[i].ToString(), pro[i].ToString()));
}
}
/// <summary>
/// 綁定城市清單
/// </summary>
private void BindCity()
{
this.dopCityList.Items.Clear();
string[] city = ws.getSupportCity(dopProvinceList.SelectedValue);
for (int i = 1; i <= Int32.Parse(city[0]); i++)
{
this.dopCityList.Items.Add(new ListItem(city[i].ToString(), city[i].ToString()));
}
}
/// <summary>
/// 擷取天氣
/// </summary>
private void GetWeather()
{
string[] mystr = ws.getWeatherbyCityName(this.dopCityList.SelectedValue, theDayFlagEnum.Today);
lblCity.Text = mystr[1].ToString();
lblWeather.Text = mystr[2].ToString();
lblTemperature.Text = mystr[3].ToString();
lblWind.Text = mystr[4].ToString();
lblDate.Text = mystr[5].ToString();
imgWeather.ImageUrl = mystr[6].ToString();
}
protected void dopProvinceList_SelectedIndexChanged(object sender, EventArgs e)
{
this.BindCity();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
this.GetWeather();
}
}
}
4.顯示界面:
是不是很簡單呢?