Web Service学习笔记:天气预报Web服务的使用方法

学了一段时间的Web服务,今天做了一个Web服务,利用YAHOO的公开天气API做自己的Web服务,主要是想练练手。现在把过程和心得分享给大家。

 

文章在我小站的地址:Web Service学习笔记:天气预报Web服务的使用方法

 

前天写了一篇文章,Web Service学习笔记:利用YAHOO公开API做天气预报Web服务 (对于本文中有不理解的地方,请参见本文),今天我就来说说如何使用Web服务(以使用这个天气服务为例子)。

 

首先要知道Web服务的地址:http://www.h2bbs.com/Weather/weather.asmx

 

下面就来说说使用的过程:

(1)首先创建一个WebApplication,做个前台页面,如下图:



 

为了方便大家,把代码也贴出来,前台代码如下:

 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>天气预报</title>
    
<style type="text/css">
        table
{clear: both; width: 60%; font: 12px Verdana; text-align: center; border:1px solid #eeeeee;}
        .titletd
{padding: 5px; color: #000000; height:24px; font-weight:bold; background-color:#eeeeee;}
        th
{height:26px; padding: 7px 5px 6px 5px; color: #ffffff; background: #456285;}
        .itemtd
{padding: 5px; color: #000000; border-bottom: 1px solid #eeeeee; height:24px;}
        .altertd
{padding: 5px; color: #000000; background-color:#eeeeee; border-bottom: 1px solid #eeeeee; height:24px;}
    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div style="text-align:center;">
    
<asp:Repeater ID="rptWeather" runat="server">
        
<HeaderTemplate>
            
<table cellpadding="0" cellspacing="0" border="0">
                
<tr>
                    
<td colspan="5" class="titletd">H2站长论坛 天气预报中心</td>
                
</tr>
                
<tr>
                    
<th>日期</th>
                    
<th>星期</th>
                    
<th>天气</th>
                    
<th>最低温度</th>
                    
<th>最高温度</th>
                
</tr>
        
</HeaderTemplate>
        
<ItemTemplate>
                 
<tr>
                    
<td class="itemtd"><%# Eval("Date")%></td>
                    
<td class="itemtd"><%# Eval("Week")%></td>
                    
<td class="itemtd"><%# Eval("Weather")%></td>
                    
<td class="itemtd"><%# Eval("Tlow")%></td>
                    
<td class="itemtd"><%# Eval("Thigh")%></td>
                
</tr>               
        
</ItemTemplate>
        
<AlternatingItemTemplate>
                  
<tr>
                    
<td class="altertd"><%# Eval("Date")%></td>
                    
<td class="altertd"><%# Eval("Week")%></td>
                    
<td class="altertd"><%# Eval("Weather")%></td>
                    
<td class="altertd"><%# Eval("Tlow")%></td>
                    
<td class="altertd"><%# Eval("Thigh")%></td>
                
</tr>               
        
</AlternatingItemTemplate>
        
<FooterTemplate>     
            
</table>
        
</FooterTemplate>
    
</asp:Repeater>
    
<table>
            
<tr>
                
<td class="itemtd">
                    请选择要查询的城市:
                
</td>
                
<td colspan="2" class="itemtd">
                    
<asp:DropDownList ID="ddlSelectCity" runat="server" AutoPostBack="True" onselectedindexchanged="ddlSelectCity_SelectedIndexChanged">
                        
<asp:ListItem Selected="True" Value="None">请选择要查询的城市</asp:ListItem>
                        
<asp:ListItem Value="0008">北京</asp:ListItem>
                        
<asp:ListItem Value="0133">天津</asp:ListItem>
                        
<asp:ListItem Value="0044">杭州</asp:ListItem>
                        
<asp:ListItem Value="0448">合肥</asp:ListItem>
                        
<asp:ListItem Value="0116">上海</asp:ListItem>
                        
<asp:ListItem Value="0031">福州</asp:ListItem>
                        
<asp:ListItem Value="0017">重庆</asp:ListItem>
                        
<asp:ListItem Value="0097">南昌</asp:ListItem>
                        
<asp:ListItem Value="0049">香港</asp:ListItem>                            
                    
</asp:DropDownList>
                
</td>
                
<td colspan="2" class="itemtd">
                    现在是 
<b><asp:Label ID="lblCityWeather" runat="server" Text="北京"></asp:Label></b> 的天气情况
                
</td>
            
</tr>
        
</table>   
    
</div>
    
</form>
    
</body>
</html>

 

 

(2)编写后台,后台代码如下:

 

        protected void Page_Load(object sender, EventArgs e)
        
{
            
if (!IsPostBack)
            
{
                H2BBS.Weather.WeatherWebService getWeather 
= new GetWebService.H2BBS.Weather.WeatherWebService();
                rptWeather.DataSource 
= getWeather.GetWeather("北京");
                rptWeather.DataBind();
            }

        }


        
protected void ddlSelectCity_SelectedIndexChanged(object sender, EventArgs e)
        
{
            
if (ddlSelectCity.SelectedValue == "None")
            
{
                Page.ClientScript.RegisterStartupScript(GetType(), 
"""<script>window.alert('请选择要查询的城市')</script>");
            }

            
else
            
{
                H2BBS.Weather.WeatherWebService getWeather 
= new GetWebService.H2BBS.Weather.WeatherWebService();
                rptWeather.DataSource 
= getWeather.GetWeather(ddlSelectCity.SelectedItem.ToString());
                rptWeather.DataBind();
                lblCityWeather.Text 
= ddlSelectCity.SelectedItem.ToString();
            }

        }

 

 

(3)生成网站,浏览页面,就成功了!比较简单,我也就不多废话了!
posted @ 2008-10-16 20:50  瞪着你的小狗  阅读(1733)  评论(6编辑  收藏  举报