Traditional Culture Encyclopedia - Weather forecast - How to use API of China Meteorological Bureau to develop weather APP?

How to use API of China Meteorological Bureau to develop weather APP?

Preparatory work:

First, apply for API (get appid and private_key).

Second, the interpretation of SmartWeatherAPI

Third, prepare areaid, type, date, appid and urlencode($key) (note that the encryption key here needs encodeurl to become part of the interface link).

Well, the code is this:

1. Find the areaid of the place you want from the attachment's areaid_list, and select the weather type you want to query.

ns string * areaid = @ " 10 10 10 100 ";

NSString *type =

@ " index _ f

/**

* document update data type number

* index: index_f (basic interface); Index_v (universal interface)

3-day forecast: forecast_f (basic interface); Prediction _v (universal interface)

*

*/

2. Get the current weather date

NSDate

* _ date =[ns date date];

NSDateFormatter *dateFormatter =

[[nsdate formatter alloc]init];

[Date Formatter]

setdate format:@ " yyyymmdd hhmmss "]; //Pay attention to the format of the date.

NSString *date =

[[Date Formatter String Date: _ Date]

substring index: 12]; //Use a 24-hour 60-minute clock accurate to the minute.

3. appid and private_key of the application.

NSString *appid =

@ " 15ds 45s 13a 465s "; //This is made up by the landlord at will.

NSString *private_key =

@ " 46s 4ds _ SmartWeatherAPI _ 45s 44d 6 "; //It's also made up.

4. Calculate the key after the urlencode. This step is more important and there are many steps. Please read it patiently.

On the basis of the original, it is calculated in PHP environment. The code is as follows, which can be found in "

/data/? areaid = 10 10 10 100 & amp; Type = index _ f & Date = 201409041509 & appid = 15ds 45s 13a 465s”,

" 46s4ds_SmartWeatherAPI_45s44d6 ",

True));

Firstly, the method of obtaining public_key and API is defined, and the method of encodeurl operation on key is defined.

Note that the methods here are all defined by me in getTime's class, followed by instantiation in main.

//get publicky

- (NSString*)

get public key:(ns string *)areaid:(ns string *)type:(ns string *)date

:(NSString*)appid {

NSString *Key = [[NSString alloc]

initWithFormat:@"/data/? areaid = % @ & amptype = % @ & ampdate = % @ & ampappid=%@ ",

Areaid, type, [date substring index: 12], appid];

return

Key;

}

//Get the complete API

-(ns string *)getAPI:(ns string *)areaid

:(ns string *)type:(ns string *)date:(ns string *)appid:(ns string *)key

{

NSString *API = [[NSString alloc]

initWithFormat:@"/data/? areaid = % @ & amptype = % @ & ampdate = % @ & ampappid = % @ & ampkey=%@ ",

areaid,type,[date substring index: 12],[appid substring index:6],

Key];

//-What is needed here is only the first 6 digits of appid! ! !

return

API

}

//will get the key progressive urlencode operation.

-(String

*)stringByEncodingURLFormat:(ns string *)_ key {

NSString *encodedString

= (__bridge NSString

*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)_key,

nil,(CFStringRef) @ "! Dollar &' () *+,-./:; =? @_~%#[]",

kcfstringencodingutf 8);

//Because of the existence of ARC, the conversion here needs to add __bridge, and I don't know why. Ask the great god to explain.

return

encodedString

}

The key point is that the algorithm under oc is as follows, remember to attach Base64.h

Add it to and reference it to the project.

//encrypt publickey and privatekey.

- (NSString *)

hmacsha 1:(ns string *)public _ key:(ns string *)private _ key {

NSData*

secretData = [private_key

data using encoding:nsu TF 8 string encoding];

NSData* stringData = [public_key

data using encoding:nsu TF 8 string encoding];

const void* keyBytes =

[secretData bytes];

const void * dataBytes =[string data

Bytes];

///# define CC _ sha 1 _ DIGEST _ LENGTH 20/* DIGEST

Byte length */

void* outs =

malloc(CC _ sha 1 _ DIGEST _ LENGTH);

CCHmac(kCCHmacAlgSHA 1,keyBytes,

[secretData length],dataBytes,[stringData length],outs);

//

Solution 1

ns data * signature data =[ns data dataWithBytesNoCopy:outs

LENGTH:CC _ sha 1 _ DIGEST _ LENGTH free hendone:YES];

return

[Signature data]

base 64 encoded string];

}

This is just a preliminary key, which has not been recognized by encodeurl, and the link cannot be recognized by the browser, so the _key obtained by the algorithm can now get the real key with one more step.

ns string * _ key =[getTime hmacsha 1:[getTime

get public key:areaid:type:date:appid]:private _ key];

NSString *key =

[getTime

stringByEncodingURLFormat:_ key];

Last step! Splicing API

ns string * weather API =[getTime getAPI:areaid:type:date

:appid

:key];

//Ok, our API can be used.

Finally, the value returned through the API is a JSON file, and we can get the desired data through parsing. Let's take a developed interface as an example.

ns dictionary * weather DIC =[getTime

getWeatherDic:@ "/data/city info/ 10 10 10 100 . html "];

The data stored in WeatherDIC dictionary is also a typical word, and the key value is taken from it.

NSDictionary

*weatherInfo = [weatherDic

objectForKey:@ " weather info "];

NSLog(@ "Today is% @% @ The weather condition is:% @% @-

%@ ",[newDateOne substring with range:nsmakerage(0,4)],[new date one

substringWithRange:NSMakeRange(4,2)],[newDateOne

substringWithRange:NSMakeRange(6,2)],[weatherInfo

objectForKey:@"weather"],[weatherInfo objectForKey:@ " temp 1 "],[weather info

objectForKey:@ " temp 2 "]);

Output: 20 14-09-04 23:40:23.243

Weather App [5688: 201108] Today is 20 14-09-04, and the weather conditions are: sunny 17℃-30℃.

The contents in the weatherInfo dictionary are -> {"weatherinfo":{"city ":"Beijing ","city id ":"1010100 ","temp1.