Exemplo n.º 1
0
        /// <summary>
        /// 仅在第一次初始化时执行
        /// </summary>
        public async void OnFirstLoad()
        {
            Geoposition Position;

            try
            {
                Position = await GetPositionAsync();
            }
            catch (InvalidOperationException)
            {
                WeatherCtr.Error(ErrorReason.Location);
                return;
            }
            catch (Exception)
            {
                WeatherCtr.Error(ErrorReason.NetWork);
                return;
            }

            //将获取到的GPS位置发送至百度地图逆地址解析服务
            float  lat    = (float)Position.Coordinate.Point.Position.Latitude;
            float  lon    = (float)Position.Coordinate.Point.Position.Longitude;
            string URL    = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=qrTMQKoNdBj3H6N7ZTdIbRnbBOQjcDGQ&output=json&coordtype=wgs84ll&location=" + lat + "," + lon;
            string Result = await GetWebResponseAsync(URL);

            if (Result != "")
            {
                //异步运行以在完成诸多解析任务的同时保持UI响应能力
                await Task.Run(async() =>
                {
                    var WeatherInfo = await GetWeatherInfoAsync(GetDistrictByAnalysisJSON(Result));
                    if (WeatherInfo == null)
                    {
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            WeatherCtr.Error(ErrorReason.APIError);
                        });
                        return;
                    }

                    //status=200时表示请求成功
                    if (WeatherInfo.status == 200)
                    {
                        WeatherDataGenarated?.Invoke(null, new WeatherData(WeatherInfo.data, jp.result.addressComponent.city + jp.result.addressComponent.district));
                    }
                    else
                    {
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            WeatherCtr.Error(ErrorReason.APIError);
                        });
                    }
                });
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 向URL指定的网络服务器发出HTTP请求,获取返回服务器返回信息
        /// </summary>
        /// <param name="url">网络服务器地址</param>
        /// <returns>网络服务器返回的信息</returns>
        private async Task <string> GetWebResponseAsync(string url)
        {
            string     strBuff = string.Empty;
            Uri        HttpURL = new Uri(url);
            WebRequest httpReq = WebRequest.Create(HttpURL);

            try
            {
                WebResponse httpResp = await httpReq.GetResponseAsync();

                Stream       respStream       = httpResp.GetResponseStream();
                StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);
                strBuff = await respStreamReader.ReadToEndAsync();
            }
            catch (WebException)
            {
                WeatherCtr.Error(ErrorReason.NetWork);
            }
            return(strBuff);
        }