今天看到公司的一个原来的网站天气预报的那个web服务好像死了,然后就抽时间自己看了下,别看这个功能貌似简单,但是对于我来说,却也弯弯绕了下,不管怎么说,也算是一种积累。废话不多说,代码:
前台:
<head runat="server"> <title>天气预报~</title> </head> <body> <form id="form1" runat="server"> 地名: <asp:TextBox ID="TB_DIMING" Text="北京" runat="server"></asp:TextBox> <asp:Button ID="Btn_Tijiao" runat="server" Text="提交" OnClick="Btn_Tijiao_Click" /> <div style=" 300px; height: 110px; border: 1px solide #ccc"> <table style=" 100%; height: 80px;"> <tr> <td style=" 30%;"> <table> <tr> <td> <asp:Literal ID="Literal1" runat="server"></asp:Literal> </td> </tr> <tr> <td> <b><asp:Literal ID="Literal2" runat="server"></asp:Literal></b> </td> </tr> </table> </td> <td> <table> <tr> <td> <b>温度:</b><asp:Literal ID="Literal3" runat="server"></asp:Literal> </td> </tr> <tr> <td> <b>风力:</b><asp:Literal ID="Literal4" runat="server"></asp:Literal> </td> </tr> <tr> <td> <b>空气质量:</b><asp:Literal ID="Literal5" runat="server"></asp:Literal> </td> </tr> </table> </td> </tr> </table> </div> </form> </body>
前台页面挺糙的,这个我知道,主要是说这个功能实现的过程:
后台:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { disp_weather("北京"); } } public void Btn_Tijiao_Click(object sender, EventArgs e) { string diming = TB_DIMING.Text.Trim(); if (diming != "") disp_weather(diming); } private void disp_weather(string city) { try { string[] arrAy = null; ArrayList weather = Bnn_Weather.Get_3day_weather(city); if (weather.Count > 0) { DateTime dt = Convert.ToDateTime(weather[3].ToString()); Literal5.Text = weather[5].ToString().Replace("空气质量:", ""); arrAy = weather[7].ToString().Split(' '); Literal2.Text = arrAy[1].ToString(); Literal3.Text = weather[8].ToString(); Literal4.Text = weather[9].ToString(); if (weather[10].ToString() == weather[11].ToString()) { Literal1.Text = "<img src="../App_Themes/tqimg/b_" + weather[10].ToString() + "" width="50" height="46" alt='" + arrAy[0].ToString() + "" + weather[1].ToString() + "天气预报:" + arrAy[1].ToString() + "' />"; } else { Literal1.Text = "<img src="../App_Themes/tqimg/b_" + weather[10].ToString() + "" width="50" height="46" alt='" + arrAy[0].ToString() + "" + weather[1].ToString() + "天气预报:" + arrAy[1].ToString() + "'/><img src="../App_Themes/tqimg/b_" + weather[11].ToString() + "" width="50" height="46" alt='" + arrAy[1].ToString().Split('转')[1].ToString() + "'/>"; } } } catch (Exception) { Page.ClientScript.RegisterClientScriptBlock(GetType(), "asdf", "<script>alert('您提供的城市查不到')</script>"); } } public class Bnn_Weather { public static ArrayList Get_3day_weather(string CityName) { try { //接口地址:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx WeatherWS wc = new WeatherWS(); string[] strarr = wc.getWeather(CityName, ""); ArrayList list = new ArrayList(); int listLength = strarr.Length; if (listLength > 0) { for (int i = 0; i < listLength; i++) { list.Add(strarr[i]); } } return list; } catch (Exception e) { throw; } } }
之前百度了个例子是在工程里添加一个WebService,后来同事告我一个方法就是通过VS2010中的命令提示工具:WSDL http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx 然后会在本地输出一个类文件,其实性质是一样的,只是我不知道。
慢慢积累….
对了,还有就是附加上这个里面需要的天气图片: