Exemple #1
0
        /// <summary>
        /// 获取心知天气
        /// </summary>
        /// <param name="city">城市</param>
        /// <returns></returns>
        private WeatherInfo GetSeniverseWeather(string city)
        {
            if (string.IsNullOrEmpty(city))
            {
                return(null);
            }
            WeatherInfo weatherInfo = new WeatherInfo()
            {
                results = new List <WeatherResult>()
            };

            try
            {
                string requestUrl = "https://api.seniverse.com/v3/weather/now.json?";
                string param      = $"key={apikey}&location={city}&language={language}&unit=c";
                requestUrl += param;
                string response         = HttpClientHelper.Get(requestUrl);
                var    seniverseWeather = SerializeHelper.SerializeJsonToObject <SeniverseWeather>(response);
                if (seniverseWeather == null)
                {
                    return(null);
                }
                if (seniverseWeather.results == null || seniverseWeather.results.Length <= 0)
                {
                    return(null);
                }
                foreach (var item in seniverseWeather.results)
                {
                    WeatherResult weatherResult = new WeatherResult();
                    weatherResult.country         = item.location.country;
                    weatherResult.id              = item.location.id;
                    weatherResult.name            = item.location.name;
                    weatherResult.path            = item.location.path;
                    weatherResult.timezone        = item.location.timezone;
                    weatherResult.timezone_offset = item.location.timezone_offset;
                    weatherResult.last_update     = item.last_update;
                    weatherResult.text            = item.now.text;
                    weatherResult.temperature     = item.now.temperature + "℃";
                    string code  = item.now.code;
                    byte[] bytes = FileReadWriteHelper.ReadBytesFromBitmap(GetBitmap(code));
                    weatherResult.icon = Base64Helper.Encoding(bytes);
                    weatherInfo.results.Add(weatherResult);
                }
            }
            catch (System.Exception ex)
            {
                LogHelper.WriteError(ex);
            }
            return(weatherInfo);
        }
Exemple #2
0
        /// <summary>
        /// 获取Jirengu天气(备用查询天气方案(地级市也可查询))
        /// </summary>
        /// <param name="city">城市</param>
        /// <returns></returns>
        private WeatherInfo GetJirenguWeather(string city)
        {
            if (string.IsNullOrEmpty(city))
            {
                return(null);
            }
            WeatherInfo weatherInfo = new WeatherInfo()
            {
                results = new List <WeatherResult>()
            };

            try
            {
                string requestUrl     = $"http://api.jirengu.com/getWeather.php?city={city}";
                string response       = HttpClientHelper.Get(requestUrl);
                var    jirenguWeather = SerializeHelper.SerializeJsonToObject <JirenguWeather>(response);
                if (jirenguWeather == null)
                {
                    return(null);
                }
                if (jirenguWeather.results == null || jirenguWeather.results.Length <= 0)
                {
                    return(null);
                }
                foreach (var item in jirenguWeather.results)
                {
                    int           i             = 0;
                    WeatherResult weatherResult = new WeatherResult();
                    weatherResult.path = item.currentCity;
                    weatherResult.text = item.weather_data[i].weather;

                    string tempStr     = item.weather_data[i].date;
                    Regex  reg         = new Regex(@"实时:(.+)℃\)");
                    Match  match       = reg.Match(tempStr);
                    string temperature = match.Groups[1].Value;
                    weatherResult.temperature = temperature + "℃";
                    string code  = GetWeatherCode(item.weather_data[i].weather);
                    byte[] bytes = FileReadWriteHelper.ReadBytesFromBitmap(GetBitmap(code));
                    weatherResult.icon = Base64Helper.Encoding(bytes);
                    weatherInfo.results.Add(weatherResult);
                    i++;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(ex);
            }
            return(weatherInfo);
        }