雅虎在iPhone应用程序中的天气

时间:2011-12-08 16:51:33

标签: iphone yahoo-weather-api

我正在开发使用雅虎气象服务的iphone应用程序(我有一把钥匙)。 我有2个问题:

  1. 我可以在我的应用程序中使用它进行商业用途(比如在appstore中免费发布我的应用程序或不使用)
  2. 为什么xml和json结果不同: http://weather.yahooapis.com/forecastrss?w=29330057&u=chttp://weather.yahooapis.com/forecastjson?w=29330057&u=c
  3. 有什么事要做(第一个有想要的位置)? 谢谢。

2 个答案:

答案 0 :(得分:1)

我怀疑这是XML命名空间的问题。根据所使用的框架和实际的完整XML,您必须按名称空间访问元素。您可能希望切换到另一个基于DOM的框架(不使用NSXMLParser),例如Google的GDataXMLNode。在基于DOM的框架中,您可以以树状结构访问各个节点,而不是自己构建一个节点。

网上有很多例子,例如Building an RSS readerHow to read and write XML documents with GDataXML。但要快速举例说明这看起来如何:

NSError *error = nil;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];

if (doc == nil) { return nil; }

NSMutableDictionary *result = [[NSMutableDictionary alloc] init];

NSArray *lists = [doc nodesForXPath:@"/result/list" error:nil];
if ([lists count] > 0)
{
    for (GDataXMLNode *list in lists) {
        int listid = [self integerInNode:list forXPath:@"listid"];
        NSString *listname = [self stringInNode:list forXPath:@"name"];

        [result setValue:[NSNumber numberWithInt:listid] forKey:listname];   

    }     
}
[doc release];
return [result autorelease]; 

答案 1 :(得分:0)

  1. 是的,雅虎!让您在公平使用政策下使用他们的API,甚至是商业用途。不要成为屁股并给他们足够的道具,例如他们的图标或徽标以及他们网站的链接。
  2. 我不认为知道为什么两种输出格式都存在差异很重要。使用更好/更容易的东西。我个人更喜欢使用JSON和Apple的NSJSONSerialization类。