private void GetGetWeather(string cityName = "") { picWeather.SizeMode = PictureBoxSizeMode.StretchImage; wEntity = GetCityWeather(cityName); if (wEntity.data != null) { lblCity.Visible = true; lblTemputure.Visible = true; lblWeather.Visible = true; var data = wEntity.data.forecast[0]; var type = data.@type; panelWeather.Visible = true; lblCity.Text = wEntity.data.city; lblWeather.Text = $"{data.@type}/{data.fengxiang}"; lblTemputure.Text = data.high + "/" + data.low; if (type.IndexOf("多云") > -1 || type.IndexOf("晴") > -1) { picWeather.Image = (Image)Properties.Resources.ResourceManager.GetObject("晴"); } else if (type.IndexOf("雨") > -1) { picWeather.Image = (Image)Properties.Resources.ResourceManager.GetObject("雨"); } else if (type.IndexOf("阴") > -1) { picWeather.Image = (Image)Properties.Resources.ResourceManager.GetObject("阴"); } toolTip1.SetToolTip(picWeather, wEntity.data.ganmao); } }
/// <summary> /// 天气接口 /// </summary> /// <param name="cityName"></param> /// <returns></returns> public static WeatherAPIEntity GetCityWeather(string cityName = "上海") { WeatherAPIEntity entity = new WeatherAPIEntity(); try { System.Net.HttpWebResponse response = HttpWebResponseUtility.CreateGetHttpResponse("http://wthrcdn.etouch.cn/weather_mini?city=" + $"{HttpUtility.UrlEncode(cityName)}", null, null, null); string jsonString = HttpWebResponseUtility.getResponseString(response); entity = JsonConvert.DeserializeObject <WeatherAPIEntity>(jsonString); } catch { } return(entity); }
public ActionResult GetWeatherbyCityName(string CityName) { using (ResponseResult <DtoGetWeather> result = new ResponseResult <DtoGetWeather>()) { if (!string.IsNullOrWhiteSpace(CityName)) { string url = ConfigHelper.GetAppSetting(DataKey.WeatherUrl); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("city", CityName); string apiresult = HttpRequestHelper.sendGet(url, dic); //反序列化json字符串 WeatherAPIEntity apiEntiey = JsonHelper.FromJsonTo <WeatherAPIEntity>(apiresult); if (apiEntiey != null) { if (apiEntiey.status == "1000") { result.IsSuccess = apiEntiey.status == "1000" ? true : false; result.Message = apiEntiey.desc; result.Entity.temperature = apiEntiey.data != null ? apiEntiey.data.wendu : ""; result.Entity.cityname = apiEntiey.data != null ? apiEntiey.data.city : ""; result.Entity.weathertype = apiEntiey.data.forecast[0].type; for (int i = 0; i < 5; i++) { result.Entity.future.Add(new BaseWeather());//先初始化天气类和初始化两天的天气集合 result.Entity.future[i].wind_strength = apiEntiey.data.forecast[i].fengli; result.Entity.future[i].wind_direction = apiEntiey.data.forecast[i].fengxiang; result.Entity.future[i].high_temperature = apiEntiey.data.forecast[i].high; result.Entity.future[i].low_temperatur = apiEntiey.data.forecast[i].low; result.Entity.future[i].weathertype = apiEntiey.data.forecast[i].type; result.Entity.future[i].dateweek = apiEntiey.data.forecast[i].date; } } else { result.IsSuccess = true; result.Message = "没有获得此位置的天气数据"; } } else { result.Message = "应用程序繁忙"; } return(Json(result)); } else { return(Json(result)); } } }
public ActionResult GetWeatherbyCityCode(string location) { using (ResponseResult <DtoGetWeather> result = new ResponseResult <DtoGetWeather>()) { if (!string.IsNullOrWhiteSpace(location)) { try { #region 获得城市名称 //查询名称的api地址 string selecturl = ConfigHelper.GetAppSetting(DataKey.BaiduGeocoderUrl); Dictionary <string, string> citydic = new Dictionary <string, string>(); //百度的ak string AK = ConfigHelper.GetAppSetting(DataKey.baiduAK); citydic.Add("ak", AK); citydic.Add("location", location); citydic.Add("output", "json"); //请求获得名称的地址 string resultjson = HttpRequestHelper.sendGet(selecturl, citydic); int cityindex = resultjson.IndexOf("city"); int districtindex = resultjson.IndexOf("district"); string tempname = resultjson.Substring(cityindex, districtindex - cityindex); //city":"成都市"," int shiindex = tempname.IndexOf("市"); string CityName = tempname.Substring(7, shiindex - 7); #endregion #region 获得天气信息 //天气接口地址未确定 string url = ConfigHelper.GetAppSetting(DataKey.WeatherUrl); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("city", CityName); string apiresult = HttpRequestHelper.sendGet(url, dic); #endregion //反序列化json字符串 WeatherAPIEntity apiEntiey = JsonHelper.FromJsonTo <WeatherAPIEntity>(apiresult); if (apiEntiey != null) { if (apiEntiey.status == "1000") { result.IsSuccess = apiEntiey.status == "1000" ? true : false; result.Message = apiEntiey.desc; result.Entity.temperature = apiEntiey.data != null ? apiEntiey.data.wendu : ""; result.Entity.cityname = apiEntiey.data != null ? apiEntiey.data.city : ""; result.Entity.weathertype = apiEntiey.data.forecast[0].type; for (int i = 0; i < 5; i++) { result.Entity.future.Add(new BaseWeather());//先初始化天气类和初始化两天的天气集合 result.Entity.future[i].wind_strength = apiEntiey.data.forecast[i].fengli; result.Entity.future[i].wind_direction = apiEntiey.data.forecast[i].fengxiang; result.Entity.future[i].high_temperature = apiEntiey.data.forecast[i].high; result.Entity.future[i].low_temperatur = apiEntiey.data.forecast[i].low; result.Entity.future[i].weathertype = apiEntiey.data.forecast[i].type; result.Entity.future[i].dateweek = apiEntiey.data.forecast[i].date; } } else { result.IsSuccess = true; result.Message = "没有获得此位置的天气数据"; } } else { result.Message = "应用程序繁忙"; } return(Json(result)); } catch (Exception ex) { result.Message = ex.Message; return(Json(result)); } } else { return(Json(result)); } } }