码迷,mamicode.com
首页 > Windows程序 > 详细

学习使用API

时间:2016-11-02 01:05:29      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:选择   属性   byte   inf   编码   api   base   get   data   

学习使用获取天气信息API和获取地理编码API制作一个简易天气信息获取器(高德API)

首先建立一个用来获取返回相关信息的类OpenWeatherMapProxy。

由于我选择返回数据类型为Json,所以我先把返回的Json利用Json2Csharp.com自动生成相关信息的属性。然后两个方法

GetAdcode和GetWeather来回去返回信息中adcode和weatherinfo。
public class OpenWeatherProxy
    {
        public static async Task<RootObject> GetAdcode(string s)
        {
            var http = new HttpClient();
            var response = await http.GetAsync("http://restapi.amap.com/v3/geocode/geo?address="+s+"&key=自己的开发ID&output=json");
            var result = await response.Content.ReadAsStringAsync();
            var serializer = new DataContractJsonSerializer(typeof(RootObject));

            var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
            var data = (RootObject)serializer.ReadObject(ms);

            return data;
        }
        public static async Task<RootObject> GetWeather(string s)
        {
            var http = new HttpClient();
            var response = await http.GetAsync("http://restapi.amap.com/v3/weather/weatherInfo?key=自己的开发ID&city=" + s + "&extension=base&output=json");
            var result = await response.Content.ReadAsStringAsync();
            var serializer = new DataContractJsonSerializer(typeof(RootObject));

            var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
            var data = (RootObject)serializer.ReadObject(ms);

            return data;
        }

        [DataContract]
        public class RootObject
        {
            [DataMember]
            public string status { get; set; }
            [DataMember]
            public string count { get; set; }
            [DataMember]
            public string info { get; set; }
            [DataMember]
            public string infocode { get; set; }
            [DataMember]
            public List<Geocode> geocodes { get; set; }
            [DataMember]
            public List<Life> lives { get; set; }
        }

        [DataContract]
        public  class Building
        {
            [DataMember]
            public List<object> name { get; set; }
            [DataMember]
            public List<object> type { get; set; }
        }

        [DataContract]
        public class Geocode
        {
            [DataMember]
            public string formatted_address { get; set; }
            [DataMember]
            public string province { get; set; }
            [DataMember]
            public string citycode { get; set; }
            [DataMember]
            public string city { get; set; }
            [DataMember]
            public string district { get; set; }
            [DataMember]
            public List<object> township { get; set; }
            [DataMember]
            public Neighborhood neighborhood { get; set; }
            [DataMember]
            public Building building { get; set; }
            [DataMember]
            public string adcode { get; set; }
            [DataMember]
            public List<object> street { get; set; }
            [DataMember]
            public List<object> number { get; set; }
            [DataMember]
            public string location { get; set; }
            [DataMember]
            public string level { get; set; }

        }

        [DataContract]
        public class Neighborhood
        {
            [DataMember]
            public List<object> name { get; set; }
            [DataMember]
            public List<object> type { get; set; }
        }

        [DataContract]
        public class Life
        {
            [DataMember]
            public string province { get; set; }
            [DataMember]
            public string city { get; set; }
            [DataMember]
            public string adcode { get; set; }
            [DataMember]
            public string weather { get; set; }
            [DataMember]
            public string temperature { get; set; }
            [DataMember]
            public string winddirection { get; set; }
            [DataMember]
            public string windpower { get; set; }
            [DataMember]
            public string humidity { get; set; }
            [DataMember]
            public string reporttime { get; set; }
        }
    }

 

学习使用API

标签:选择   属性   byte   inf   编码   api   base   get   data   

原文地址:http://www.cnblogs.com/G33k/p/6021024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!