首页 > 代码库 > 学习使用API
学习使用API
学习使用获取天气信息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 =http://www.mamicode.com/ (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 =http://www.mamicode.com/ (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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。