iOS: 目前天气API 整理

因天气预报服务,发现网上天气预报接口和环境数据接口方面资料比较乱,而且有不少内容已经失效,就整理下部分资料吧。

 

这里有一份知乎的回答可以参考 :(时间点2013-12-31)

http://www.zhihu.com/question/20521716

一、 接口基本固定到一个时间点无法使用,大部分接口已经失效

二、中国气象数据开放平台简直不能吐槽了,登录后总是返回刚才访问的页面,然后就跳到未登录界面,一直来回跳,就是登不了

 

对于(一)问题是有解决办法,方法来自@酷小孩的文章http://www.cnblogs.com/babycool/p/3575167.html 感激不尽

下面是用oc 语言写的获取方法

#define CITYWEATHERURL(Identifier) [NSString stringWithFormat:@"http://mobile.weather.com.cn/data/forecast/%@.html", Identifier]


NSURLCache *urlCache = [NSURLCache sharedURLCache];
  [urlCache setMemoryCapacity:1*1024*1024];
  NSURL *url = [NSURL URLWithString:CITYWEATHERURL(cityIdentifier)];
  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
  [request setCachePolicy:NSURLRequestReloadRevalidatingCacheData];
  [request setURL:url];
  [request setHTTPMethod:@"GET"];
  [request setValue:@"application/json, text/javascript, */*; q=0.01" forHTTPHeaderField:@"Accept"];
  [request setValue:@"gzip" forHTTPHeaderField:@"Accepts-Encoding"];;
  [request setValue:@"zh-CN,zh;q=0.8" forHTTPHeaderField:@"Accept-Language"];
  [request setValue:@"http://mobile.weather.com.cn/" forHTTPHeaderField:@"Referer"];
  [request setTimeoutInterval:20];
  //缓存
  NSCachedURLResponse *responseU = [urlCache cachedResponseForRequest:request];
  if (responseU != nil) {
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
  }
  [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

    if ([data length] > 0 && connectionError == nil) {
      NSDictionary *jsonString = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
     //do something
      
    } else if ([data length] == 0 && connectionError ==nil) { // 没有数据
      
    } else if (connectionError != nil) {
      NSDictionary *jsonString = [NSJSONSerialization JSONObjectWithData:[responseU data] options:NSJSONReadingMutableLeaves error:nil];
       //do something
    } else {
    }
  }];

对于问题(二)无能为力

至于第三方天气服务尝试发现部分并不靠谱也可能我用的是免费版的吧,还有聚合数据服务可以尝试一下,但我并没有使用,不知道是否可行

posted @ 2015-07-12 10:30  折半  阅读(1274)  评论(0编辑  收藏  举报