コード例 #1
0
        public async Task <Weather4> GetWeatherAsync()
        {
            string url = "http://api.openweathermap.org/data/2.5/forecast?";

            //string url = "http://api.openweathermap.org/data/2.5/forecast?q=Moscow&appid=19cfe7ddf6b7b30677251b4ea4f0c6c0&units=metric&lang=ru";
            if (CrossSettings.Current.GetValueOrDefault("UseGPS", false) == false)
            {
                url += "q=" + Editor1.Text;
            }
            else
            {
                Location loacation = await GetLocationAsync();

                if (loacation == null)
                {
                    await DisplayAlert("Ошибка :(", "Не удалось получить местоположение", "Ok");

                    return(null);
                }
                else
                {
                    url += "lat=" + Convert.ToString(loacation.Latitude) + "&lon=" + Convert.ToString(loacation.Longitude);
                }
            }
            url += "&appid=19cfe7ddf6b7b30677251b4ea4f0c6c0&units=metric&lang=ru";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string          output;
                using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                {
                    output = streamReader.ReadToEnd();
                }
                Weather4 weather = JsonConvert.DeserializeObject <Weather4>(output);
                return(weather);
            }
            catch (WebException error)
            {
                try
                {
                    HttpWebResponse httpWebError = (HttpWebResponse)error.Response;
                    if (httpWebError.StatusCode == HttpStatusCode.NotFound)
                    {
                        await DisplayAlert("Ошибка :(", "Не удалось найти город.", "Ok");
                    }
                    else
                    {
                        await DisplayAlert("Ошибка :(", error.Message, "Ok");
                    }
                }
                catch (NullReferenceException)
                {
                    await DisplayAlert("Ошибка :(", "Не удалось установить соединение.", "Ok");
                }
                return(null);
            }
        }
コード例 #2
0
        private async Task UpdateWeatherAsync() // обновление погоды и обновление погоды на giu
        {
            Indicator.IsRunning = true;
            Weather4 weather4 = await GetWeatherAsync();

            string   Temperature, Status, Icon;
            DateTime Time;

            if (weather4 != null)
            {
                Label1.Text = weather4.City.Name;
                WeatherStack.Children.Clear();
                int ListSize = weather4.WeatherList.Length;
                for (int i = 0; i < ListSize; i++)
                {
                    if (i % CrossSettings.Current.GetValueOrDefault("WeatherStep", 8) == 0)
                    {
                        Temperature = Convert.ToInt32(weather4.WeatherList[i].MainInfo.Temp).ToString() + "°C";
                        Status      = weather4.WeatherList[i].CommonWeather[0].Description;
                        Time        = Convert.ToDateTime(weather4.WeatherList[i].Time);
                        Icon        = weather4.WeatherList[i].CommonWeather[0].Icon;
                        if (CrossSettings.Current.GetValueOrDefault("WeatherStep", 8) == 8)
                        {
                            Create_Frame(Temperature, Status, Time.ToString("dd MMMM"), Icon);
                        }
                        else
                        {
                            Create_Frame(Temperature, Status, Time.ToString("dd MMMM HH:mm"), Icon);
                        }
                    }
                }
            }
            else
            {
                Label1.Text = "Error";
            }
            Editor1.Text        = "";
            Indicator.IsRunning = false;
        }