Asp.net 调用 天气预报接口实现三天的天气显示
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 id="Head1" runat="server">
<title></title>
<style type="text/css">
.bigTitle
{
color: #FF0066;
font-size: x-large;
}
.style2
{
font-weight: 700;
background-color: #FFFFFF;
}
.style3
{
background-color: #6699FF;
}
.style4
{
background-color: #FF66FF;
}
.style5
{
font-weight: 700;
color: #FF0000;
background-color: #FFFFFF;
}
.style6
{
font-size: xx-large;
}
</style>
</head>
<body>
<form id="formWeather" runat="server">
<div style="width: 1000px; background-color: #FFFFFF; padding: 15px;">
<table width="100%" border="0" cellpadding="0" cellspacing="0"
style="background-color: #FFFFFF">
<tr>
<td height="60" align="center"
style="font-weight: 700; color: #00FFFF; background-color: #669999">
<asp:Label Font-Bold="true" ID="Title" runat="server" CssClass="bigTitle"
Text="国内外主要城市 3天天气预报实例" />
<br />
提供大家学习交流`</td>
</tr>
<tr>
<td>
<table width="100%" border="0" cellspacing="2" cellpadding="0"
style="background-color: #FFFFCC">
<tr>
<td width="15%" height="30" class="style3" style="text-align: right">
用户选择:</td>
<td align="right" class="style3" style="text-align: left">
省份:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Height="20px" onselectedindexchanged="DropDownList1_SelectedIndexChanged"
Width="147px">
</asp:DropDownList>
城市:<asp:DropDownList ID="DropDownList2" runat="server" Height="20px" Width="140px">
</asp:DropDownList>
<asp:Button ID="Button2" runat="server" οnclick="Button2_Click" Text="查询" />
</td>
</tr>
<tr>
<td width="15%" height="30" class="style4" style="text-align: right">
手动输入:</td>
<td align="right" class="style4" style="text-align: left">
城市:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="查询" />
</td>
</tr>
<tr>
<td width="15%" height="30" class="style2">
</td>
<td align="right" style="text-align: center">
<strong>
<asp:Label CssClass="bredfont" ID="disp" runat="server"
style="font-size: xx-large" />
<span class="style6">三天气温如下:</span></strong></td>
</tr>
<tr>
<td valign="top" class="style2">
<strong>今日实况</strong>
</td>
<td class="hfont">
<asp:Label ID="lab_sk" runat="server" CssClass="style2" />
</td>
</tr>
<tr>
<td valign="top" class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td valign="top" class="style2">
<span class="style2"><strong>今天</strong></span>
</td>
<td class="hfont">
<asp:Label ID="today" runat="server" CssClass="style2" />
<asp:Image AlternateText="icon" ID="today1" ImageAlign="AbsMiddle"
runat="server" />
<asp:Image AlternateText="icon" ID="today1_2" ImageAlign="AbsMiddle"
runat="server" />
</td>
</tr>
<tr>
<td valign="top" class="style2">
<strong>今天指数</strong>
</td>
<td>
</td>
</tr>
<tr>
<td valign="top" class="style5">
<strong>温馨提示:</strong></td>
<td>
</td>
</tr>
<tr>
<td valign="top" class="style2">
</td>
<td valign="top" class="style2">
<asp:Label ID="Label5" runat="server" CssClass="style2" />
</td>
</tr>
<tr>
<td valign="top" class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td valign="top" class="style2">
<span class="style3">48小明</span>
</td>
<td class="hfont">
<asp:Label ID="one_day" runat="server" CssClass="style2" />
<asp:Image AlternateText="icon" ID="today2_1" ImageAlign="AbsMiddle"
runat="server" />
<asp:Image AlternateText="icon" ID="today2_2" ImageAlign="AbsMiddle"
runat="server" />
</td>
</tr>
<tr>
<td valign="top" class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td valign="top" class="style2">
<span class="style4"><strong>72小明</strong></span>
</td>
<td class="hfont">
<asp:Label ID="tow_day" runat="server" CssClass="style2" />
<asp:Image AlternateText="icon" ID="today3_1" ImageAlign="AbsMiddle"
runat="server" />
<asp:Image AlternateText="icon" ID="today3_2" ImageAlign="AbsMiddle"
runat="server" />
</td>
</tr>
<tr>
<td valign="top" class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td valign="top" class="style2">
<strong>城市介绍</strong>
</td>
<td valign="top" class="style2">
</td>
</tr>
<tr>
<td colspan="2" valign="top">
<asp:Image ID="city_photo" ImageAlign="Left" runat="server" Width="400px"
Height="383px" />
<asp:Label ID="city_introduct" runat="server" CssClass="style2" />
</td>
</tr>
<tr>
<td height="30" colspan="2" align="right" valign="bottom"
style="background-color: #FF6600">
<strong>预报时间</strong>:
<asp:Label ID="datatime" runat="server" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台代码 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
three_day_weather.WeatherWebServiceSoapClient weather = new three_day_weather.WeatherWebServiceSoapClient("WeatherWebServiceSoap");
DataSet ds=weather.getSupportDataSet();
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataValueField = "id";
DropDownList1.DataTextField = "zone";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "选择省份");
disp_weather("沧州");
DropDownList2.Items.Add("选择城市");
}
}
/// lsit[0] 所属省
/// list[1] 城市
/// list[2] 城市编号
/// list[3] 图片
/// list[4] 为天气预报时间
/// list[5] 当天天气情况
/// list[6] 温度
/// list[7] 风力情况
/// list[10] 今日实况
/// list[8] 当天天气图片
// list[9] 当天天气图片
// list[15] 第二天天气图片 (名称)
// list[16] 第二天天气图片
// list[20] 第三天天气图片
// list[21] 第三天天气图片
// list[11] 温馨提示
/// list[12] 第二天天气情况
/// list[13] 第二天温度
/// list[14] 风力情况
/// list[17] 第三天天气情况
/// list[18] 第三温度
/// list[19] 风力情况
/// list[22] 为城市信息
/// list[23] 返回作者
/// 本DLL文件来源于河软youkuaiyun.com 3 :QQ:979170768
/// 优快云博客: http://write.blog.youkuaiyun.com/postlist
/// dll文件采用返回xml文件来写的
/// 您也可以采用网上提供的返回来的数据等很多方法
/// 2011,12,18
/// </summary>
/// <param name="CityName"></param>
/// <returns></returns>
protected void Province_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void City_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
disp_weather(TextBox1.Text.Trim());
}
private void disp_weather(string city)
{
try
{
ArrayList weather = weather = BNN_Weather.Bnn_Weather.Get_3day_weather(city);
lab_sk.Text = weather[10].ToString();
today.Text = weather[6] + " " + weather[5] + " " + weather[7];
one_day.Text = weather[13] + " " + weather[12] + " " + weather[14];
tow_day.Text = weather[18] + " " + weather[17] + " " + weather[19];
Label5.Text = weather[11].ToString().Replace("\n", "<br />");
city_introduct.Text = weather[22].ToString().Replace("\n", "<br />");
datatime.Text = DateTime.Parse(weather[4].ToString()).ToString("yyyy年MM月dd日 dddd HH:mm");
disp.Text = weather[0] + " / " + weather[1];
today1.ImageUrl = "~/weather/weather/" + weather[8];
today1_2.ImageUrl = "~/weather/weather/" + weather[9];
today2_1.ImageUrl = "~/weather/weather/" + weather[15];
today2_2.ImageUrl = "~/weather/weather/" + weather[16];
today3_1.ImageUrl = "~/weather/weather/" + weather[20];
today3_2.ImageUrl = "~/weather/weather/" + weather[21];
city_photo.ImageUrl = "http://www.cma.gov.cn/tqyb/img/city/" + weather[3];
}
catch (Exception)
{
Page.ClientScript.RegisterClientScriptBlock(GetType(), "asdf", "<script>alert('您提供的城市查不到')</script>");
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
three_day_weather.WeatherWebServiceSoapClient weather = new three_day_weather.WeatherWebServiceSoapClient("WeatherWebServiceSoap");
string[] ds = weather.getSupportCity(DropDownList1.SelectedItem.Text);
DropDownList2.Items.Clear();
foreach (string item in ds)
{
DropDownList2.Items.Add(item.Substring(0,item.LastIndexOf('(')));
}
DropDownList2.Items.Insert(0, "选择城市");
}
protected void Button2_Click(object sender, EventArgs e)
{
disp_weather(DropDownList2.SelectedItem.Text);
}
}
Dll代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;
namespace BNN_Weather
{
public class Bnn_Weather
{
/// <summary>
/// CityName为用户当前要查寻的城市名称
/// 本方法返回一个数据组
/// CityPhoto.ImageUrl = "http://www.cma.gov.cn/tqyb/img/city/" + list[3]; //这里的list[3]是城市的图片
/// lsit[0] 所属省
/// list[1] 城市
/// list[2] 城市编号
/// list[3] 图片
/// list[4] 为天气预报时间
/// list[5] 当天天气情况
/// list[6] 温度
/// list[7] 风力情况
/// list[10] 今日实况
/// list[8] 当天天气图片
// list[9] 当天天气图片
// list[15] 第二天天气图片 (名称)
// list[16] 第二天天气图片
// list[20] 第三天天气图片
// list[21] 第三天天气图片
// list[11] 温馨提示
/// list[12] 第二天天气情况
/// list[13] 第二天温度
/// list[14] 风力情况
/// list[17] 第三天天气情况
/// list[18] 第三温度
/// list[19] 风力情况
/// list[22] 为城市信息
/// list[23] 返回作者
/// 本DLL文件来源于河软youkuaiyun.com 3 :979170768
/// 优快云博客: http://write.blog.youkuaiyun.com/postlist
/// </summary>
/// <param name="CityName"></param>
/// <returns></returns>
public static ArrayList Get_3day_weather(string CityName)
{
XmlTextReader xt = new XmlTextReader("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=" + CityName);
int i = 0;
ArrayList list = new ArrayList();
while (xt.Read())
{
if (xt.NodeType == XmlNodeType.Text)
{
list.Add(xt.Value);
}
}
list.Add("本DLL文件来源于河软youkuaiyun.com 3 :979170768+++优快云博客: http://write.blog.youkuaiyun.com/postlist");
return list;
}
}
}
QQ:979170768
白宁宁
2011,12,18
河软优快云
优快云博客: http://write.blog.youkuaiyun.com/postlist
百度博客: http://hi.baidu.com/979170768/blog