Yahoo API Integration?

半城伤御伤魂 提交于 2019-11-30 15:21:46

Here is a subset of code using the XML portion of Yahoo! Answers. I had written this to write my own answers app.

    NSString *question =  @"Who won the 1975 World Series?";
    NSString *address = @"http://answers.yahooapis.com/AnswersService/V1/questionSearch?appid=iQuestion&query=";
    NSString *request = [NSString stringWithFormat:@"%@%@",address,question];
    NSURL *URL = [NSURL URLWithString:request];
    NSError *error;    
    NSString *XML = [NSString stringWithContentsOfURL:URL encoding:NSASCIIStringEncoding error:&error];

    // Extract current answer the 'dirty' way
    NSString *answer = [[[[XML componentsSeparatedByString:@"<ChosenAnswer>"] 
    objectAtIndex:1] componentsSeparatedByString:@"</ChosenAnswer>"] objectAtIndex:0];
    NSLog(@"%@", answer);

The XML extraction is very crude, and if you would the best alternative is to use an XMLParser or XMLDocument as opposed to doing a String extrapolation. It's kinda ghetto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!