Пример #1
0
        //This method gets weather data from the openweathermap API, JSON data is then parsed into
        //a class for use in other parts of the program.
        //-----------------------------------------getWeather(string zip)----------------------------------------
        public WeatherObject getWeather(string zip)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://api.openweathermap.org/data/2.5/");
                string weather_API_key = ConfigurationManager.AppSettings["WeatherAPI"];

                for (int i = 0; i < 4; i++)
                {
                    HttpResponseMessage response = client.GetAsync("weather?zip=" + zip + "&APPID=" + weather_API_key).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        string result = response.Content.ReadAsStringAsync().Result;
                        //Debug.WriteLine(result);
                        WeatherObject weatherInfo = JsonConvert.DeserializeObject <WeatherObject>(result);
                        return(weatherInfo);
                    }
                    else
                    {
                        Debug.WriteLine("Unsuccsessful request. Status code: " + response.StatusCode);
                        Thread.Sleep(2000 * i);
                    }
                }
            }
            return(null);
        }
Пример #2
0
        private string getCurrentWeather(string zipCode)
        {
            weatherAPICall weather = new weatherAPICall();

            if (weather == null)
            {
                return("clear");
            }

            WeatherObject weatherInfo = weather.getWeather(zipCode);

            if (weatherInfo == null)
            {
                return("clear");
            }

            return(weatherInfo.weather[0].main);
        }
Пример #3
0
        private int getCurrentTemp(string zipCode)
        {
            weatherAPICall weather = new weatherAPICall();

            if (weather == null)
            {
                return(50);
            }

            WeatherObject weatherInfo = weather.getWeather(zipCode);

            if (weatherInfo == null)
            {
                return(50);
            }

            float kelvinTemp = weatherInfo.main.temp;
            int   temp       = (int)((kelvinTemp - 273.15) * 1.8 + 32);

            return(temp);
        }