今天看到公司的一個原來的網站天氣預報的那個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="width: 300px; height: 110px; border: 1px solide #ccc"> <table style="width: 100%; height: 80px;"> <tr> <td style="width: 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.ClientScrxml.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 然后會在本地輸出一個類文件,其實性質是一樣的,只是我不知道。
慢慢積累….
對了,還有就是附加上這個里面需要的天氣圖片:
http://files.VEVb.com/houlin/tqimg.zip

新聞熱點
疑難解答