void getWeather(string city) { using (WebClient web = new WebClient()) { string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}&units=metric&cnt=6", city, APPID); var json = web.DownloadString(url); var result = JsonConvert.DeserializeObject <WeatherInfo.Root>(json); WeatherInfo.Root outPut = result; lbl_cityName.Text = string.Format("{0}", outPut.name); lbl_country.Text = string.Format("{0}", outPut.sys.country); lbl_Temp.Text = string.Format("{0} \u00B0" + "C", outPut.main.temp); picture_Main.Image = setIcon(outPut.weather[0].icon); } }
void GetWeather(string city, string apiKey) { using (WebClient web = new WebClient()) { string url = string.Format("https://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}", city, apiKey); var jsonString = web.DownloadString(url); var resultJson = JsonConvert.DeserializeObject <WeatherInfo.Root>(jsonString); WeatherInfo.Root output = resultJson; lbl_cityName1.Text = string.Format("{0}", output.name); lbl_country.Text = string.Format("{0}", output.sys.country); lbl_temp.Text = string.Format("{0} \u00B0" + "C", Math.Round(Convert.ToInt16(output.main.temp) - 273.15)); lbl_mainConditions.Text = string.Format("{0}", output.weather[0].main); lbl_mainDescription.Text = string.Format("{0}", output.weather[0].description); lbl_mainSpeed.Text = string.Format("{0} km/h", output.wind.speed); object mainImage = Resources.ResourceManager.GetObject("_" + output.weather[0].icon); mainPicture.Image = (Image)mainImage; } }
public async void GetWeatherAsync(string cityName) { string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}&units=metric&cnt=6", cityName, APPID); var http = new HttpClient(); var respone = await http.GetAsync(url); var data = await respone.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <WeatherInfo.Root>(data); WeatherInfo.Root outPut = result; city.Text = string.Format("{0}", outPut.name); CountryName.Text = string.Format("{0}", outPut.sys.country); Temp.Text = string.Format("Temperature: {0} \u00B0" + "C", outPut.main.temp); windSpeed.Text = string.Format("Speed: {0} km/h", outPut.wind.speed); //weather temperature string imageurl = string.Format("http://openweathermap.org/img/w/{0}.png", outPut.weather[0].icon); image.Source = new BitmapImage(new Uri(imageurl, UriKind.Absolute)); }