public WeatherInfo GetWeatherFor(string city)
        {
            string apiKey = "ab597816349d0acfa405e0e434b6644e";
            string Url    = $"http://api.openweathermap.org/data/2.5/weather?appid={apiKey}&units=metric";

            using (var client = new WebClient())
            {
                string      json        = client.DownloadString(Url + $"&q={city}");
                WeatherInfo weatherInfo = JsonConvert.DeserializeObject <WeatherInfo>(json);
                return(weatherInfo);
            }
        }
示例#2
0
        private PeriodWeatherModel GetPeriodWeatherModel(PartsOfDay title, WeatherInfo item)
        {
            PeriodWeatherModel period = new PeriodWeatherModel
            {
                Title       = title,
                Description = item.Weather[0].Description,
                Humidity    = item.Main.Humidity,
                Clouds      = item.Clouds.All,
                WindSpeed   = item.Wind.Speed,
                Temperature = Convert.ToInt32(item.Main.Temp),
                Date        = item.Dt_txt,
                GroundLevel = item.Main.Grnd_level,
                SeaLevel    = item.Main.Sea_level,
                TempMax     = Convert.ToInt32(item.Main.Temp_max),
                TempMin     = Convert.ToInt32(item.Main.Temp_min),
                Pressure    = item.Main.Pressure
            };

            return(period);
        }
示例#3
0
        public async Task <PeriodWeatherModel> GetCurrentWeatherByCity(int id)
        {
            try
            {
                string templateUrl = $"{Constants.ApiUrl}/weather/?id={id}&appid={Constants.ApiKey}";
                var    degreeType  = await SecureStorage.GetAsync("DegreeType");

                if (degreeType == DegreeType.Celsius.ToString())
                {
                    templateUrl = $"{templateUrl}&units=metric";
                }
                WeatherInfo response = await _httpClientHelper.PerformGet <WeatherInfo>(templateUrl);

                PeriodWeatherModel periodWeatherModel = GetPeriodWeatherModel(PartsOfDay.defaultPart, response);
                return(periodWeatherModel);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
        }