问题描述:
使用C#做一个简易的天气预报系统
问题解决:
主要使用类如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using WeatherReport.ServiceReference1; namespace WeatherReport { class WeatherLoc { //调用天气接口的类private static WeatherWebServiceSoapClient ws= new WeatherWebServiceSoapClient("WeatherWebServiceSoap"
); public WeatherLoc() { }
//查看天气接口支持的省份 public String[] GetSupportProvince() { String[] supportProv = ws.getSupportProvince(); return supportProv; }
//查看天气情况支持的城市 public String[] GetSupportCity(String province) { return ws.getSupportCity(province); }
//查看指定城市的天气信息 public WeatherInfo GetWeatherInfo(String city) { String[] allInfo = ws.getWeatherbyCityName(city); WeatherInfo weather = new WeatherInfo(); weather.Province = allInfo[0]; weather.CityName = allInfo[1]; weather.LastUpdateTime = allInfo[4]; weather.Temperature = allInfo[5]; weather.WeatherLive = allInfo[10]; weather.LivingIndex = allInfo[11]; weather.CityInfo = allInfo[22]; FutureInfo[] future = new FutureInfo[3]; //今天天气信息 future[0].temp = allInfo[5]; future[0].overView = allInfo[6]; future[0].windDirection = allInfo[7]; future[0].tendencyBeg = allInfo[8]; future[0].tendencyEnd = allInfo[9]; //明天的天气信息 future[1].temp = allInfo[12]; future[1].overView = allInfo[13]; future[1].windDirection = allInfo[14]; future[1].tendencyBeg = allInfo[15]; future[1].tendencyEnd = allInfo[16]; //后天的天气信息 future[2].temp = allInfo[17]; future[2].overView = allInfo[18]; future[2].windDirection = allInfo[19]; future[2].tendencyBeg = allInfo[20]; future[2].tendencyEnd = allInfo[21]; weather.FutureInfo = future; return weather; } } }
WeatherInfo类:包含查询天气情况信息
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WeatherReport { //未来三天天气情况结构体,C#中结构体默认访问权限是private,C++结构体默认访问权限为public struct FutureInfo { public String temp; //温度 public String overView; //概况 public String windDirection; //风向和风力 public String tendencyBeg; //趋势开始图片 public String tendencyEnd; //趋势结束图片 }; class WeatherInfo { private String province; //省份 private String cityName; //城市名称 private String lastUpdateTime; //最后更新时间 private String temperature; //温度 private FutureInfo[] info; //未来三天天气情况 private String weatherLive; //现在天气实况 private String livingIndex; //生活指数 private String cityInfo; //城市介绍 public WeatherInfo() { cityName = ""; lastUpdateTime = ""; temperature = ""; info = new FutureInfo[3]; weatherLive = ""; livingIndex = ""; cityInfo = ""; } public String Province { get { return province; } set { province = value; } } public FutureInfo[] FutureInfo { get { return info; } set { info = value; } } public String CityName { get { return cityName; } set { cityName = value; } } public String LastUpdateTime { get { return lastUpdateTime; } set { lastUpdateTime = value; } } public String Temperature { get { return temperature; } set { temperature = value; } } public String WeatherLive { get { return weatherLive; } set { weatherLive = value; } } public String LivingIndex { get { return livingIndex; } set { livingIndex = value; } } public String CityInfo { get { return cityInfo; } set { cityInfo = value; } } } }
具体使用中国气象局天气接口方法如下:
(1)添加服务引用
(2)包含服务器引用接口
(3)接口使用,参考WeatherLoc类中接口调用
执行效果: