C# 天气预报

网上有很多资料,可是比较零散,我整理了一下 

这里我用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; }
}
}


好了,看结果:

C# 天气预报

对了程序用到的图片可以在那个网站上下载,还没写完,所以界面好丑,有空再继续写吧

因为没写完,想要代码的童鞋跟我说一下吧,我就不全部贴出来了。 

 


 

 

 

 

转载于:https://www.cnblogs.com/-Lei/archive/2012/03/04/2379129.html