public OpenWeather.OpenWeather GetOpenWeatherByCity(string city) { try { string appid = "e67f327620e3cc056d936f8d1fbf20fd"; string url = $"http://api.openweathermap.org/data/2.5/weather?units=metric&q={city}&appid={appid}"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) { string line; if ((line = streamReader.ReadToEnd()) != null) { OpenWeather.OpenWeather weather = JsonConvert.DeserializeObject <OpenWeather.OpenWeather>(line); return(weather); } } } catch { MessageBox.Show("Failed to connect to the service.Check your internet connection.You may need a VPN.", "Warning"); } return(null); }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { label16.Text = "Status: defining weather data for a selected city..."; label16.ForeColor = Color.Gray; string city = Convert.ToString(comboBox1.SelectedItem); Task.Run(() => { OpenWeather.OpenWeather openWeather = weather.GetOpenWeatherByCity(city); if (openWeather != null) { coordinate = new GeoCoordinate(openWeather.Coord.Lat, openWeather.Coord.Lon); openWeatherOneAPI = weather.GetWeather(coordinate); if (InvokeRequired) { Invoke(new Action(() => SetParameters())); } else { SetParameters(); } } else { label16.Text = "Status: city geodata not defined."; label16.ForeColor = Color.Red; } }); }
public async void handler(String queue) { WebRequest request = WebRequest.Create(queue); request.Method = "POST"; request.ContentType = "application/x-www-urlencoded"; try { WebResponse response = await request.GetResponseAsync(); string answer = string.Empty; using (Stream s = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(s)) { answer = await reader.ReadToEndAsync(); } } response.Close(); an = answer; OpenWeather.OpenWeather oW = JsonConvert.DeserializeObject <OpenWeather.OpenWeather>(answer); city = oW.name; label8.Text = this.city; tempF = (oW.main.temp - 32) * 5 / 9; tempC = oW.main.temp; panel1.BackgroundImage = oW.weather[0].Icon; label1.Text = oW.weather[0].main; label2.Text = oW.weather[0].description; if (trig) { label3.Text = "Средняя температура: " + oW.main.temp.ToString("0.##") + "°С"; } else { label3.Text = "Средняя температура: " + tempF.ToString("0.##") + "°F"; } label6.Text = "Скорость: " + oW.wind.speed.ToString() + " м/с"; label7.Text = "Направление: " + oW.wind.deg.ToString() + "°"; label4.Text = "Влажность: " + oW.main.humidity.ToString() + "%"; label5.Text = "Давление: " + ((int)oW.main.pressure).ToString() + " мм рт.ст."; } catch (System.Net.WebException e) { MessageBox.Show(e.Message + "Город не найден"); } }
private async void button1_Click(object sender, EventArgs e) { try { //Program.cityName = textBox1.Text; //for russian description paste "&lang=ru" before &APPID panel1.Text = "Weather in " + Program.cityName; // не работает ???? Program.cityName = textBox1.Text; string str = "http://api.openweathermap.org/data/2.5/weather?q=" + Program.cityName + "&APPID=1d0fd92330221c38bcc652af7481a5a1"; WebRequest request = WebRequest.Create(str); request.Method = "POST"; request.ContentType = "application/x-www-urlencoded"; WebResponse responce = await request.GetResponseAsync(); string answer = ""; //or String.Empty(); using (Stream s = responce.GetResponseStream()) { using (StreamReader reader = new StreamReader(s)) { answer = await reader.ReadToEndAsync(); } } responce.Close(); richTextBox1.Text = answer; OpenWeather.OpenWeather oW = JsonConvert.DeserializeObject <OpenWeather.OpenWeather>(answer); panel1.BackgroundImage = oW.weather[0].Icon; label1.Text = oW.weather[0].main; label2.Text = oW.weather[0].description; label3.Text = "Avg Temperature, Celsium: " + oW.main.temp.ToString("0.0"); label9.Text = "Feels like, Celsium: " + oW.main.feels_like.ToString("0.0"); label4.Text = "Humidity, %: " + oW.main.humidity.ToString(); label5.Text = "Pressure, mm: " + ((int)oW.main.pressure).ToString(); label6.Text = "Speed, km/h: " + ((int)oW.wind.speed).ToString(); label7.Text = "Direction (from): " + oW.wind.direction(oW.wind.deg); } catch { MessageBox.Show("Loading failed..."); } }
private async void Form1_Load(object sender, EventArgs e) { WebRequest request = WebRequest.Create("http://api.openweathermap.org/data/2.5/weather?q=Moscow&APPID=YourKey"); request.Method = "POST"; request.ContentType = "application/x-www-urlencoded"; WebResponse response = await request.GetResponseAsync(); string answer = string.Empty; using (Stream s = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(s)) { answer = await reader.ReadToEndAsync(); } } response.Close(); richTextBox1.Text = answer; OpenWeather.OpenWeather oW = JsonConvert.DeserializeObject<OpenWeather.OpenWeather>(answer); panel1.BackgroundImage = oW.weather[0].Icon; label1.Text = oW.weather[0].main; label2.Text = oW.weather[0].description; label3.Text = "Средния температура (°C): " + oW.main.temp.ToString("0.##"); label6.Text = "Скорость (м/с): " + oW.wind.speed.ToString(); label7.Text = "Направление °: " + oW.wind.deg.ToString(); label4.Text = "Влажность (%): " + oW.main.humidity.ToString(); label5.Text = "Давление (мм): " + ((int)oW.main.pressure).ToString(); }
private async void Main_UI_Load(object sender, EventArgs e) { labelforselect.Select(); myHttpWebRequest = (HttpWebRequest)WebRequest.Create(urlForStart); try { await Task.Run(() => GetInfoFromUrl.SetInf(testUrl, out owForecast)); myHttpWebResponse = (HttpWebResponse)await myHttpWebRequest.GetResponseAsync(); string answer = string.Empty; using (Stream s = myHttpWebResponse.GetResponseStream()) { using (StreamReader reader = new StreamReader(s)) { answer = await reader.ReadToEndAsync(); } } myHttpWebResponse.Close(); OpenWeather.OpenWeather ow = JsonConvert.DeserializeObject <OpenWeather.OpenWeather>(answer); weather = DataHendler.GetArrayTemp(owForecast, 9); // - масив для температуры data = DataHendler.GetArrayData(owForecast, 9); // - масив для времени pictureBox1.Image = ow.weather[0].Icon; label22.Text = ow.weather[0].description; label21.Text = ow.main.temp.ToString("0.##") + "℃"; label23.Text = ow.wind.speed.ToString(); label32.Text = ow.main.humidity.ToString(); label24.Text = ((int)ow.main.pressure).ToString(); label18.Text = ow.name.ToString(); label61.Text = DataHendler.SplitDataData(owForecast.list[0].dt_txt); label49.Text = DataHendler.SplitDataData(owForecast.list[8].dt_txt); label33.Text = DataHendler.SplitDataData(owForecast.list[16].dt_txt); label43.Text = DataHendler.SplitDataData(owForecast.list[24].dt_txt); label28.Text = owForecast.list[8].wind.speed.ToString(); label58.Text = owForecast.list[8].main.humidity.ToString(); label30.Text = ((int)owForecast.list[8].main.pressure).ToString(); label45.Text = (GetMinTemp(owForecast, 8, 17)).ToString("0.##") + "℃"; label26.Text = (GetMaxTemp(owForecast, 8, 17)).ToString("0.##") + "℃"; label40.Text = owForecast.list[16].wind.speed.ToString(); label59.Text = owForecast.list[16].main.humidity.ToString(); label37.Text = ((int)owForecast.list[16].main.pressure).ToString(); label36.Text = (GetMinTemp(owForecast, 16, 25)).ToString("0.##") + "℃"; label41.Text = (GetMaxTemp(owForecast, 16, 25)).ToString("0.##") + "℃"; label55.Text = owForecast.list[24].wind.speed.ToString(); label60.Text = owForecast.list[24].main.humidity.ToString(); label52.Text = ((int)owForecast.list[24].main.pressure).ToString(); label51.Text = (GetMinTemp(owForecast, 24, 33)).ToString("0.##") + "℃"; label56.Text = (GetMaxTemp(owForecast, 24, 33)).ToString("0.##") + "℃"; } catch (WebException ex) { MessageBox.Show($"Погода не найдена, проверте правильность данных.\n{ex.Status}", "Увага!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (Exception ex) { MessageBox.Show($"Ошибка. Код ошибки: \n{ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
public static void SetInf(string originUrl, out OpenWeather.OpenWeather weather) { Adaptee adaptee = new Adaptee(); weather = JsonConvert.DeserializeObject <OpenWeather.OpenWeather>(adapter.GetData(adaptee, originUrl)); }
private void SetParameters() { if (openWeatherOneAPI != null) { // Get OpenWeather to get info about the city (OpenWeatherOneCallAPI doesn't have it). GeoCoordinate coordinate = new GeoCoordinate(openWeatherOneAPI.Lat, openWeatherOneAPI.Lon); OpenWeather.OpenWeather openWeather = weather.GetOpenWeatherByCoord(coordinate); // Set current forecast. MainLabel.Text = "Condition: " + openWeatherOneAPI.Current.Weather[0].Main; DescriptionLabel.Text = "Description: " + openWeatherOneAPI.Current.Weather[0].Description; TemperatureLabel.Text = "Temperature: " + openWeatherOneAPI.Current.Temp + " ℃, feels like " + openWeatherOneAPI.Current.Feels_Like + " ℃"; CityLabel.Text = "City: " + openWeather.Name + ", " + openWeather.Sys.Country; WeatherPicture.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Current.Weather[0].Icon}.png"); DateLabel.Text = "Date: " + UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Current.Dt, openWeatherOneAPI.Timezone); HumidityLabel.Text = "Humidity: " + Convert.ToString(openWeatherOneAPI.Current.Humidity) + "%"; PressureLabel.Text = "Pressure: " + Convert.ToString(openWeatherOneAPI.Current.Pressure) + " hPa"; SpeedLabel.Text = "Speed: " + Convert.ToString(openWeatherOneAPI.Current.Wind_Speed) + "m/s"; DegLabel.Text = "Deg: " + Convert.ToString(openWeatherOneAPI.Current.Wind_Deg) + "°"; // Set hourly forecast. pictureBox1.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Hourly[0].Weather[0].Icon}.png"); HourlyTempLabel1.Text = Convert.ToString(openWeatherOneAPI.Hourly[0].Temp) + " ℃"; groupBox5.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Hourly[0].Dt, openWeatherOneAPI.Timezone).ToString("HH:mm"); pictureBox2.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Hourly[3].Weather[0].Icon}.png"); HourlyTempLabel2.Text = Convert.ToString(openWeatherOneAPI.Hourly[3].Temp) + " ℃"; groupBox6.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Hourly[3].Dt, openWeatherOneAPI.Timezone).ToString("HH:mm"); pictureBox3.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Hourly[6].Weather[0].Icon}.png"); HourlyTempLabel3.Text = Convert.ToString(openWeatherOneAPI.Hourly[6].Temp) + " ℃"; groupBox7.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Hourly[6].Dt, openWeatherOneAPI.Timezone).ToString("HH:mm"); pictureBox4.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Hourly[9].Weather[0].Icon}.png"); HourlyTempLabel4.Text = Convert.ToString(openWeatherOneAPI.Hourly[9].Temp) + " ℃"; groupBox8.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Hourly[9].Dt, openWeatherOneAPI.Timezone).ToString("HH:mm"); pictureBox5.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Hourly[12].Weather[0].Icon}.png"); HourlyTempLabel5.Text = Convert.ToString(openWeatherOneAPI.Hourly[12].Temp) + " ℃"; groupBox9.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Hourly[12].Dt, openWeatherOneAPI.Timezone).ToString("HH:mm"); pictureBox6.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Hourly[15].Weather[0].Icon}.png"); HourlyTempLabel6.Text = Convert.ToString(openWeatherOneAPI.Hourly[15].Temp) + " ℃"; groupBox10.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Hourly[15].Dt, openWeatherOneAPI.Timezone).ToString("HH:mm"); pictureBox7.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Hourly[18].Weather[0].Icon}.png"); HourlyTempLabel7.Text = Convert.ToString(openWeatherOneAPI.Hourly[18].Temp) + " ℃"; groupBox11.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Hourly[18].Dt, openWeatherOneAPI.Timezone).ToString("HH:mm"); pictureBox8.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Hourly[21].Weather[0].Icon}.png"); HourlyTempLabel8.Text = Convert.ToString(openWeatherOneAPI.Hourly[21].Temp) + " ℃"; groupBox12.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Hourly[21].Dt, openWeatherOneAPI.Timezone).ToString("HH:mm"); // Set daily forecast. pictureBox9.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Daily[0].Weather[0].Icon}.png"); groupBox13.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Daily[0].Dt, openWeatherOneAPI.Timezone).ToString("dd.MM"); label2.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[0].Temp.Min)) + " ℃"; label3.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[0].Temp.Max)) + " ℃"; pictureBox10.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Daily[1].Weather[0].Icon}.png"); groupBox14.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Daily[1].Dt, openWeatherOneAPI.Timezone).ToString("dd.MM"); label4.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[1].Temp.Min)) + " ℃"; label5.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[1].Temp.Max)) + " ℃"; pictureBox11.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Daily[2].Weather[0].Icon}.png"); groupBox15.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Daily[2].Dt, openWeatherOneAPI.Timezone).ToString("dd.MM"); label6.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[2].Temp.Min)) + " ℃"; label7.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[2].Temp.Max)) + " ℃"; pictureBox12.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Daily[3].Weather[0].Icon}.png"); groupBox16.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Daily[3].Dt, openWeatherOneAPI.Timezone).ToString("dd.MM"); label8.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[3].Temp.Min)) + " ℃"; label9.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[3].Temp.Max)) + " ℃"; pictureBox13.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Daily[4].Weather[0].Icon}.png"); groupBox17.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Daily[4].Dt, openWeatherOneAPI.Timezone).ToString("dd.MM");; label10.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[4].Temp.Min)) + " ℃"; label11.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[4].Temp.Max)) + " ℃"; pictureBox14.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Daily[5].Weather[0].Icon}.png"); groupBox18.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Daily[5].Dt, openWeatherOneAPI.Timezone).ToString("dd.MM"); label12.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[5].Temp.Min)) + " ℃"; label13.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[5].Temp.Max)) + " ℃"; pictureBox15.Load($"http://openweathermap.org/img/w/{openWeatherOneAPI.Daily[6].Weather[0].Icon}.png"); groupBox19.Text = UnixConverter.ConvertFromUnixTimestamp(openWeatherOneAPI.Daily[6].Dt, openWeatherOneAPI.Timezone).ToString("dd.MM"); label14.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[6].Temp.Min)) + " ℃"; label15.Text = Convert.ToString(Convert.ToInt32(openWeatherOneAPI.Daily[6].Temp.Max)) + " ℃"; label16.Text = "Status: data ready."; label16.ForeColor = Color.Green; comboBox1.Enabled = true; progressBar1.Value = 0; } }