Exemple #1
0
        public async Task <OpenWeatherRoot> GetCurrentWeather(string city)
        {
            try
            {
                HttpClient client = new HttpClient();
                string     units  = "metric";
                string     apiKey = "9d3692f574b8ba802d57747928cf6442";
                string     apiURL = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&units={1}&appid={2}", city, units, apiKey);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.GetAsync(apiURL);

                if (response.IsSuccessStatusCode)
                {
                    var json = await response.Content.ReadAsStringAsync();

                    OpenWeatherRoot result = JsonConvert.DeserializeObject <OpenWeatherRoot>(json);
                    return(result);
                }
                return(null);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Exemple #2
0
        public string GetWeatherImage(OpenWeatherRoot weather)
        {
            string imageSource = string.Format("http://openweathermap.org/img/w/{0}.png", weather.Weather[0].Icon);

            return(imageSource);
        }