WindowsPhone8 自动获取 ip 以及 当前所在城市信息

WindowsPhone8 自动获取 ip 以及 当前所在城市信息

    示例代码如下:
    /// <summary>
    /// 获取当前地理信息
    /// </summary>
    public static class GetCurrentAddressInfo
    {
        #region 获取当前的地址
        /// <summary>
        /// 获取当前的地址
        /// </summary>
        public static void GetCurrentAddress()
        {
            //获取当前地址的 API
            UriBuilder urlbuilder =
               new UriBuilder("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js");

            //创建 HttpWebRequest 对象
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlbuilder.Uri);

            //异步调用
            IAsyncResult result = request.BeginGetResponse(ResponseCallback, request);
        }
        static void ResponseCallback(IAsyncResult result)
        {
            App _currentApp = (App)Application.Current;

            //获取异步返回的信息
            HttpWebRequest request = (HttpWebRequest)result.AsyncState;

            //创建 HttpWebResponse 对象
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);

            //读取文件流
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                //Json 串
                String strJson = @reader.ReadToEnd()
                .Replace("var remote_ip_info = ", "")
                .Replace(";", "");

                Byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strJson);
                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    //调用 DataContractJsonSerializer 解析Json
                    DataContractJsonSerializer serializer = new 
                    DataContractJsonSerializer(_currentApp._address.GetType());
                    _currentApp._address = (Address)serializer.ReadObject(ms);
                }
            }
        }
        #endregion
    }
    #region Address 地址类
    /// <summary>
    /// Address 地址类
    /// </summary>
    public class Address
    {
        /// <summary>
        /// id 省份或城市的编号
        /// </summary>
        public String id { get; set; }

        /// <summary>
        /// updateTime 更新时间
        /// </summary>
        public String updateTime { get; set; }

        /// <summary>
        /// Country 国家
        /// </summary>
        public String country { get; set; }

        /// <summary>
        /// Province 省份
        /// </summary>
        public String province { get; set; }

        /// <summary>
        /// City 市
        /// </summary>
        public String city { get; set; }

        /// <summary>
        /// cityPhoto 城市图片
        /// </summary>
        public String cityPhoto { get; set; }

        /// <summary>
        /// weatherInfo 今日天气实况
        /// </summary>
        public String toDayweather { get; set; }

        /// <summary>
        /// lifeInfo 生活指数
        /// </summary>
        public String lifeInfo { get; set; }

        /// <summary>
        /// cityIntroduce 城市介绍
        /// </summary>
        public String cityIntroduce { get; set; }
    }
    #endregion

源码下载地址:http://download.csdn.net/download/yimiyuangguang/6945069

以上案例仅供学习参考,如有不足之处还请提出指正,谢谢!


WindowsPhone8 自动获取 ip 以及 当前所在城市信息

上一篇:在 C# 中将 List 转换为 List


下一篇:WindowsPhon8 ScrollViewer+计时器 实现信息自动滚动播放