網上有很多資料,可是比較零散,我整理了一下
這里我用webservice進行調用來顯示天氣預報: http://www.webxml.com.cn/Webservices/WeatherWebService.asmx
網站上提供了幾個操作:
getSupportCity
查詢本天氣預報Web Services支持的國內外城市或地區信息
輸入參數:byProvinceName = 指定的洲或國內的省份,若為ALL或空則表示返回全部城市;返回數據:一個一維字符串數組 String(),結構為:城市名稱(城市代碼)。
getSupportDataSet
獲得本天氣預報Web Services支持的洲、國內外省份和城市信息
輸入參數:無;返回:DataSet 。DataSet.Tables(0) 為支持的洲和國內省份數據,DataSet.Tables(1) 為支持的國內外城市或地區數據。DataSet.Tables(0).Rows(i).Item("ID") 主鍵對應 DataSet.Tables(1).Rows(i).Item("ZoneID") 外鍵。
Tables(0):ID = ID主鍵,Zone = 支持的洲、省份;Tables(1):ID 主鍵,ZoneID = 對應Tables(0)ID的外鍵,Area = 城市或地區,AreaCode = 城市或地區代碼。
getSupportProvince
獲得本天氣預報Web Services支持的洲、國內外省份和城市信息
輸入參數:無; 返回數據:一個一維字符串數組 String(),內容為洲或國內省份的名稱。
getWeatherbyCityName
根據城市或地區名稱查詢獲得未來三天內天氣情況、現在的天氣實況、天氣和生活指數
調用方法如下:輸入參數:theCityName = 城市中文名稱(國外城市可用英文)或城市代碼(不輸入默認為上海市),如:上海 或 58367,如有城市名稱重復請使用城市代碼查詢(可通過 getSupportCity 或 getSupportDataSet 獲得);返回數據: 一個一維數組 String(22),共有23個元素。
String(0) 到 String(4):省份,城市,城市代碼,城市圖片名稱,最后更新時間。String(5) 到 String(11):當天的 氣溫,概況,風向和風力,天氣趨勢開始圖片名稱(以下稱:圖標一),天氣趨勢結束圖片名稱(以下稱:圖標二),現在的天氣實況,天氣和生活指數。String(12) 到 String(16):第二天的 氣溫,概況,風向和風力,圖標一,圖標二。String(17) 到 String(21):第三天的 氣溫,概況,風向和風力,圖標一,圖標二。String(22) 被查詢的城市或地區的介紹
下載天氣圖標(包含大、中、小尺寸) 天氣圖例說明 調用此天氣預報Web Services實例下載 (VB ASP.net 2.0)
接下來說一下具體步驟:
(1)新建一個WinForm 的工程,對着工程,右鍵->添加服務應用(英文版的是add server reference)
(2) 在地址欄輸入剛才的網址: http://www.webxml.com.cn/Webservices/WeatherWebService.asmx ,點擊前往進行驗證
(3)成功后點擊確定就能調用前面說到的操作了
以調用getWeatherbyCityName為例,輸入參數為廣州時,使用 HTTP POST 協議對操作進行測試,得到以下XML形式的數據
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/">
<string>
廣東
</string>
<string>
廣州
</string>
<string>
59287
</string>
<string>
59287.jpg
</string>
<string>
2012-3-4 11:32:43
</string>
<string>
18℃/22℃
</string>
<string>
3月4日 小雨轉中雨
</string>
<string>
無持續風向微風
</string>
<string>
7.gif
</string>
<string>
8.gif
</string>
<string>
今日天氣實況:氣溫:20℃;風向/風力:東南風 2級;濕度:87%;空氣質量:良;紫外線強度:最弱
</string>
<string>
穿衣指數:建議着薄型套裝或牛仔衫褲等春秋過渡裝。年老體弱者宜着套裝、夾克衫等。 感冒指數:天氣轉涼,空氣濕度較大,較易發生感冒,體質較弱的朋友請注意適當防護。 運動指數:有降水,較適宜在戶內開健身和休閑運動,若堅持戶外運動,注意攜帶雨具並注意避雨防滑。 洗車指數:不宜洗車,未來24小時內有雨,如果在此期間洗車,雨水和路上的泥水可能會再次弄臟您的愛車。 晾曬指數:有降水,不適宜晾曬。若需要晾曬,請在室內准備出充足的空間。 旅游指數:溫度適宜,又有小雨和微風作伴,會給您的旅行帶來意想不到的景象,適宜旅游,可不要錯過機會呦! 路況指數:有降水,路面潮濕,車輛易打滑,請小心駕駛。 舒適度指數:溫度適宜,風力不大,您在這樣的天氣條件下,會感到比較清爽和舒適。 空氣污染指數:氣象條件有利於空氣污染物稀釋、擴散和清除,可在室外正常活動。 紫外線指數:屬弱紫外線輻射天氣,無需特別防護。若長期在戶外,建議塗擦SPF在8-12之間的防曬護膚品。
</string>
可以看到<string> </string>之間就是我們想獲得的信息,是一個string類型的變量
getWeatherbyCityName 返回的是有23個元素的string數組,數組中的元素包含着上面的信息
下面是代碼:
public class WeatherServer
{
static WeatherService.WeatherWebServiceSoapClient w=
new WeatherService.WeatherWebServiceSoapClient("WeatherWebServiceSoap");
//獲得支持的省份和地區並添加到 ToolStripComboBox中
public static void GetSupportProvince(ToolStripComboBox tsCbBox)
{
String[] supportProv = w.getSupportProvince();
foreach (String s in supportProv)
{
tsCbBox.Items.Add(s);
}
}
//根據選擇的省份的名字列出該省份的城市
public static void GetSupportCity(ToolStripComboBox tsCbBox,string prov)
{
tsCbBox.Items.Clear();
string[] supportCity = w.getSupportCity(prov);
foreach (String s in supportCity)
{
string[] city=s.Split(new Char[] {'('},
StringSplitOptions.RemoveEmptyEntries);
if(city.Length>0)
tsCbBox.Items.Add(city[0]);
}
}
//獲得天氣狀況
public static Weather GetWeather(string city)
{
string[] weatherMsg=w.getWeatherbyCityName(city);
if (weatherMsg == null || weatherMsg.Length == 0)
return null;
Weather weather = new Weather();
weather.Temperature = weatherMsg[5];
weather.WeatherCondition=weatherMsg[6];
weather.WindDirection = weatherMsg[7];
weather.pictToday = Application.StartupPath
+"\\Resource\\a_"+weatherMsg[8];
weather.tmTemperature = weatherMsg[12];
weather.tmWeather=weatherMsg[13];
weather.pictTomorrow = Application.StartupPath
+ "\\Resource\\a_" + weatherMsg[15];
return weather;
}
}
這里Weather是我寫的用來存儲天氣狀況的類:
public sealed class Weather
{
private string temperature;
private string weatherCondition;
private string windDirection;
public string pictToday;
public string pictTomorrow;
public string tmTemperature;
public string tmWeather;
public string Temperature
{
get { return this.temperature; }
set { this.temperature = value; }
}
public string WeatherCondition
{
get { return this.weatherCondition; }
set { this.weatherCondition = value; }
}
public string WindDirection
{
get { return this.windDirection; }
set { this.windDirection = value; }
}
}
好了,看結果:
對了程序用到的圖片可以在那個網站上下載,還沒寫完,所以界面好丑,有空再繼續寫吧
因為沒寫完,想要代碼的童鞋跟我說一下吧,我就不全部貼出來了。