Learning to use the API

Source: Internet
Author: User

Learn to use the Get weather information API and get geocoding API to make a simple weather information collector (Gold API)

First, create a class openweathermapproxy to get information about the return.

Since I chose to return the data type as JSON, I first took the returned JSON with json2csharp.com to automatically generate the properties of the relevant information. And then two methods

Getadcode and GetWeather come back to return messages Adcode and Weatherinfo.
 Public classOpenweatherproxy { Public Static AsyncTask<rootobject> Getadcode (strings) {varHTTP =NewHttpClient (); varResponse =awaithttp. Getasync ("http://restapi.amap.com/v3/geocode/geo?address="+s+"&key= 's own development Id&output=json"); varresult =awaitResponse.            Content.readasstringasync (); varSerializer =NewDataContractJsonSerializer (typeof(Rootobject)); varms =NewMemoryStream (Encoding.UTF8.GetBytes (result)); vardata =(rootobject) serializer.            ReadObject (MS); returndata; }         Public Static AsyncTask<rootobject> GetWeather (strings) {varHTTP =NewHttpClient (); varResponse =awaithttp. Getasync ("Http://restapi.amap.com/v3/weather/weatherInfo?key= 's own development id&city="+ S +"&extension=base&output=json"); varresult =awaitResponse.            Content.readasstringasync (); varSerializer =NewDataContractJsonSerializer (typeof(Rootobject)); varms =NewMemoryStream (Encoding.UTF8.GetBytes (result)); vardata =(rootobject) serializer.            ReadObject (MS); returndata; } [DataContract] Public classRootobject {[DataMember] Public stringStatus {Get;Set; } [DataMember] Public stringCount {Get;Set; } [DataMember] Public stringinfo {Get;Set; } [DataMember] Public stringInfocode {Get;Set; } [DataMember] PublicList<geocode> Geocodes {Get;Set; } [DataMember] PublicList<life> lives {Get;Set; } } [DataContract] Public  classBuilding {[DataMember] Publiclist<Object> Name {Get;Set; } [DataMember] Publiclist<Object> Type {Get;Set; } } [DataContract] Public classGeocode {[DataMember] Public stringformatted_address {Get;Set; } [DataMember] Public stringProvince {Get;Set; } [DataMember] Public stringCitycode {Get;Set; } [DataMember] Public stringCity {Get;Set; } [DataMember] Public stringDistrict {Get;Set; } [DataMember] Publiclist<Object> Township {Get;Set; } [DataMember] PublicNeighborhood Neighborhood {Get;Set; } [DataMember] PublicBuilding Building {Get;Set; } [DataMember] Public stringAdcode {Get;Set; } [DataMember] Publiclist<Object> Street {Get;Set; } [DataMember] Publiclist<Object> Number {Get;Set; } [DataMember] Public stringLocation {Get;Set; } [DataMember] Public stringLevel {Get;Set; } } [DataContract] Public classneighborhood {[DataMember] Publiclist<Object> Name {Get;Set; } [DataMember] Publiclist<Object> Type {Get;Set; } } [DataContract] Public classLife {[DataMember] Public stringProvince {Get;Set; } [DataMember] Public stringCity {Get;Set; } [DataMember] Public stringAdcode {Get;Set; } [DataMember] Public stringWeather {Get;Set; } [DataMember] Public stringtemperature {Get;Set; } [DataMember] Public stringwinddirection {Get;Set; } [DataMember] Public stringWindpower {Get;Set; } [DataMember] Public stringHumidity {Get;Set; } [DataMember] Public stringReporttime {Get;Set; } }    }

Learning to use the API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.