public CityWeatherResponse GetByCity(string cityName)
        {
            try
            {
                var cityWeatherResponse = new CityWeatherResponse();
                var client  = new RestClient(_openWeatherMapConfig.Value.BaseEndpoint);
                var request = new RestRequest("")
                              .AddParameter("q", cityName)
                              .AddParameter("appid", _openWeatherMapConfig.Value.Token)
                              .AddParameter("units", UNIT);

                var response = client.Get(request);
                if (!response.IsSuccessful)
                {
                    _notificator.Handle(CityWeatherConstants.CITY_NOT_FOUND);
                    _logger.LogWarning(CityWeatherLogConstants.CITY_NOT_FOUND, cityName);

                    return(null);
                }

                var jsonAsString = response.Content;
                var jsonObject   = JObject.Parse(jsonAsString)["main"];
                cityWeatherResponse = jsonObject.ToObject <CityWeatherResponse>();

                return(cityWeatherResponse);
            }
            catch (Exception error)
            {
                _notificator.Handle(CityWeatherConstants.WEATHER_CLIENT_ERROR);
                _logger.LogError(error, error.Message);

                return(null);
            }
        }
Beispiel #2
0
        private async void getWeatherMessageByXML()
        {
            CityWeatherResponse myWeather = await GetWeatherByXML.GetWeather("广州");

            weatherTempNum.Text = myWeather.Results.Weather_data.Temperature[0];
        }