static int MaxReportToShow = 4; // maximum number of wheather reports to show on console static async Task RunAsnyc() { // Update port # in the following line. client.BaseAddress = new Uri("http://api.openweathermap.org/data/2.5/forecast"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); try { HttpResponseMessage response = await client.GetAsync(RequestPath); if (response.StatusCode == HttpStatusCode.OK) { CurrentWeatherRoot result = await response.Content.ReadAsAsync <CurrentWeatherRoot>(); Console.WriteLine(LogCurrentWeather(result)); } else { Console.WriteLine("Request failed. Response status code: " + response.StatusCode); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }
static string LogCurrentWeather(CurrentWeatherRoot wheaterObject) { string result = ""; result += "City name: " + wheaterObject.city.name + " \n"; result += "City id: " + wheaterObject.city.id + " \n"; result += "Wheather reports: \n"; if (wheaterObject.list != null) { for (int i = 0; i < wheaterObject.list.Count && i < MaxReportToShow; i++) { result += "\t Date: " + wheaterObject.list[i].dt_txt + " \n"; result += "\t Temperature: " + wheaterObject.list[i].main.temp + " \n"; result += "\t Humidity: " + wheaterObject.list[i].main.humidity + " \n"; if (wheaterObject.list[i].weather != null && wheaterObject.list[i].weather.Count > 0) { result += "\t Wheather: " + wheaterObject.list[i].weather[0].description + " \n"; } result += "\t --- \n"; } } return(result); }