public async Task <RootObjectF> WeatherForecastString(HttpClient cons, string name) { using (cons) { HttpResponseMessage res = await cons.GetAsync("/data/2.5/forecast?q=" + name + "&APPID=7c104bec3fd35e45bceca9de995726b4&units=metric"); res.EnsureSuccessStatusCode(); if (res.IsSuccessStatusCode) { string reader = await res.Content.ReadAsStringAsync(); RootObjectF result = JsonConvert.DeserializeObject <RootObjectF>(reader); return(result); } return(null); } }
/// <summary> /// /// </summary> /// <param name="cityName"></param> /// <returns>A newly created string with foreast</returns> public async Task <string> GetFrcstRes(string cityName) { HttpClient httpclnt = new HttpClient(); httpclnt.BaseAddress = new Uri("http://api.openweathermap.org"); httpclnt.DefaultRequestHeaders.Accept.Clear(); httpclnt.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); RootObjectF rootObject = await WeatherForecastString(httpclnt, cityName); string rsltF = ""; for (int i = 0; i < 38; i++) { rsltF += "DateTime: " + DateTimeOffset.FromUnixTimeSeconds(rootObject.list[i].dt).LocalDateTime + "\nTEMPERATURE\tMax temperature: " + rootObject.list[i].main.temp_max + "°C" + "\tMin temperature: " + rootObject.list[i].main.temp_min + "°C\n" + "\nWIND\tSpeed: " + rootObject.list[i].wind.speed + " meter/sec\tDirection: " + rootObject.list[i].wind.deg + " degrees(meteorological)\n" + "Cloudiness: " + rootObject.list[i].clouds.all + "%\n\n"; } return(rsltF); }
private async void Page_Loaded(object sender, RoutedEventArgs e) { #region -Fetch weather data from API- var position = await LocationManager.GetPosition(); RootObjectF Forecast = await WeatherForecast.Forecast(position.Coordinate.Latitude, position.Coordinate.Longitude, 5); #endregion #region -Creating and binding into weather list- List <DayWeather> WeatherList = new List <DayWeather>(); foreach (var tempDayWeather in Forecast.list) { DayWeather Dw = new DayWeather(); Dw.Icon = String.Format("ms-appx:///Assets/Weather/{0}.png", tempDayWeather.weather[0].icon); Dw.Description = tempDayWeather.weather[0].description + " | "; Dw.Temp = tempDayWeather.temp.min.ToString() + " / " + tempDayWeather.temp.max.ToString(); System.DateTime tDate = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); Dw.WeatherDate = tDate.AddSeconds(tempDayWeather.dt).ToString("dd-MMM") + " | "; WeatherList.Add(Dw); } WeatherListB.ItemsSource = WeatherList; #endregion }