Exemplo n.º 1
0
        protected void GetWeatherInfo(object sender, EventArgs e)
        {
            string appId = "c195ce001037f65bae898b8812b8ab26";

            if (txtCity.Text.ToString() == "Please Enter City Name")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please Enter City Name')", true);
            }
            else
            {
                string url = string.Format("http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&units=metric&cnt=1&APPID={1}", txtCity.Text.Trim(), appId);
                using (WebClient client = new WebClient())
                {
                    string json = client.DownloadString(url);

                    WeatherInfo weatherInfo = (new JavaScriptSerializer()).Deserialize <WeatherInfo>(json);
                    lblCity_Country.Text    = weatherInfo.city.name + "," + weatherInfo.city.country;
                    imgCountryFlag.ImageUrl = string.Format("http://openweathermap.org/images/flags/{0}.png", weatherInfo.city.country.ToLower());
                    lblDescription.Text     = weatherInfo.list[0].weather[0].description;
                    imgWeatherIcon.ImageUrl = string.Format("http://openweathermap.org/img/w/{0}.png", weatherInfo.list[0].weather[0].icon);
                    lblTempMin.Text         = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.min, 1));
                    lblTempMax.Text         = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.max, 1));
                    lblTempDay.Text         = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.day, 1));
                    lblTempNight.Text       = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.night, 1));
                    lblHumidity.Text        = weatherInfo.list[0].humidity.ToString();
                    tblWeather.Visible      = true;
                }
            }
        }
Exemplo n.º 2
0
        private void updateClick(object sender, RoutedEventArgs e)
        {
            var url = $"https://api.openweathermap.org/data/2.5/forecast?q={cityTB.Text}&units=metric&appid=9176f39c6ae3eb96104eb48077805a7a&cnt=5";

            using (WebClient client = new WebClient())
            {
                var json = client.DownloadString(url);
                weather = JsonConvert.DeserializeObject <WeatherInfo>(json);
            }
            dataGrid.ItemsSource = weather.List;
        }
Exemplo n.º 3
0
        protected void weatherQuery()
        {
            WeatherInfo   wi   = new WeatherInfo();
            TimerCallback trCB = async(object state) =>
            {
                await((WeatherInfo)state).WeatherQuery();
                bindData((WeatherInfo)state);
            };

            myTimer = new Timer(trCB, wi, 0, (int)wi.nHours * 3600000);
        }
        public async Task <WeatherInfo> GetWeather(string APPID, string ZIP)
        {
            WeatherInfo weatherInfo = new WeatherInfo {
                ZIP = ZIP
            };

            var pairs = new Dictionary <string, string>
            {
                { "zip", ZIP + ",us" },
                { "units", "imperial" },
                { "APPID", APPID }
            };

            var content = new FormUrlEncodedContent(pairs);
            var query   = content.ReadAsStringAsync().Result;

            string response;

            string temperature;
            string conditions;

            try
            {
                response = await httpClient.GetStringAsync(requestUri + "?" + query);

                JavaScriptSerializer serializer = new JavaScriptSerializer();

                object objResponse = serializer.DeserializeObject(response);

                Dictionary <string, object> mapResponse = (Dictionary <string, object>)objResponse;
                Dictionary <string, object> mapMain     = (Dictionary <string, object>)mapResponse["main"];
                object[] mapWeatherCollection           = (object[])mapResponse["weather"];
                Dictionary <string, object> mapWeather  = (Dictionary <string, object>)mapWeatherCollection[0];

                temperature = mapMain["temp"].ToString();
                conditions  = mapWeather["main"].ToString();
            }
            catch (Exception)
            {
                weatherInfo.ErrorMessage = errorHTTP;

                return(weatherInfo);
            }

            weatherInfo.Temperature = temperature;
            weatherInfo.Conditions  = conditions;

            return(weatherInfo);
        }
Exemplo n.º 5
0
        public void bindData(WeatherInfo wi)
        {
            RunOnUiThread(() =>
            {
                locationText.Text = wi.City;
                stateText.Text    = wi.State;
                atmText.Text      = wi.Temperature + "°C  " + wi.Humidity + " %  " + wi.Pressure + "hPa";
                windText.Text     = wi.WindState + " - " + wi.WindDirectionValue;
                lastUpd.Text      = wi.Date.ToShortTimeString();
                Context context   = this;
                // Get the Resources object from our context
                Resources res   = context.Resources;
                string fileName = "_" + wi.IconId;//+ ".png";

                int resId = res.GetIdentifier(fileName, "drawable", this.PackageName);
                mainImage.SetImageResource(resId);
            });
        }
        private void buttonGetInfo_Click(object sender, EventArgs e)
        {
            string url = "http://api.openweathermap.org/data/2.5/weather?q=" + textBoxCity.Text + "&appid=4e48950e61aa189901c61ad99f57a27a&units=metric";

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
            {
                textBoxServerInfo.Text = streamReader.ReadToEnd();
            }

            WeatherInfo     weatherInfo     = JsonConvert.DeserializeObject <WeatherInfo>(textBoxServerInfo.Text);
            WeatherInfoData weatherInfoData = new WeatherInfoData(weatherInfo);

            weathers.Add(weatherInfoData);

            // labelTemp.Text = weatherInfo.Main.Temp.ToString();
        }
 public WeatherInfoData(WeatherInfo w)
 {
     Temp     = w.Main.Temp;
     Name     = w.Name;
     Pressure = w.Main.Pressure;
 }