0

我正在使用 Youtube API,我想要一个搜索自动完成功能,就像 int 网站一样,当您在 iPhone 应用程序的搜索输入框中输入内容时,它会为您提供术语建议。我已经阅读了文档,但仍然缺少,这可以使用 API 吗?

4

2 回答 2

4

好吧,我知道在这里回答为时已晚,但我会发布这个答案,因为这让我疯狂了几天!!!并希望它会保存给其他人......

所以...我正在使用这个APIhttp ://suggestqueries.google.com/complete/search?client=youtube&ds=yt&alt=json&q=%@ (q 是自动完成搜索的查询)。

现在,如果您尝试打开浏览器,粘贴此 API 并将 q=%@ 更改为(假设):q=br,您会注意到一些后缀为 .js 的文件已下载到您的计算机。出于某种原因,我无法像那样解析JSON,所以我做了那个技巧:

    @property(strong, nonatomic) NSMutableArray *ParsingArray // Put that in .h file or after @interface in your .m file

  -(void)autocompleteSegesstions : (NSString *)searchWish{
//searchWish is the text from your search bar (self.searchBar.text)


NSString *jsonString = [NSString stringWithFormat:@"http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&alt=json&q=%@", searchWish];
    NSString *URLString = [jsonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // Encoding to identify where, for example, there are spaces in your query.


NSLog(@"%@", URLString);

    NSData *allVideosData = [[NSData alloc]initWithContentsOfURL:[[NSURL alloc]initWithString:URLString]];

NSString *str = [[NSString alloc]initWithData:allVideosData encoding:NSUTF8StringEncoding];
NSLog(@"%@", str); //Now you have NSString contain JSON. 
NSString *json = nil;
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner scanUpToString:@"[[" intoString:NULL]; // Scan to where the JSON begins
[scanner scanUpToString:@"]]" intoString:&json];
//The idea is to identify where the "real" JSON begins and ends.
json = [NSString stringWithFormat:@"%@%@", json, @"]]"];
NSLog(@"json = %@", json);


NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] //Push all the JSON autocomplete detail in to jsonObject array.
                                                                                 options:0 error:NULL];
self.ParsingArray = [[NSMutableArray alloc]init]; //array that contains the objects.
for (int i=0; i != [jsonObject count]; i++) {
    for (int j=0; j != 1; j++) {
        NSLog(@"%@", [[jsonObject objectAtIndex:i] objectAtIndex:j]);
        [self.ParsingArray addObject:[[jsonObject objectAtIndex:i] objectAtIndex:j]];
        //Parse the JSON here...

    }

}}   

而已。现在 ParsingArray 是包含来自 YouTube 的所有自动完成信息的数组!为了能够在用户每次单击 searchBar 上的另一个字符时更改它,请使用此函数:

            - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [self autocompleteSegesstions:self.searchBar.text];}

现在,这是您应该拥有的主要代码。要使这段代码更快(因为您现在可以看到键盘上有写入延迟),请使用另一个线程下载ParsingArray或使用Asynchronous block。(只需将第一个方法的内容插入 Async 块...)

请记住-也许还有另一种实现自动完成youTube搜索的方法比这更好,但我只是没有找到它,我搜索了很多!如果有人知道更好的方法,如果他在这里发布,我会更高兴。

玩得开心!!!

于 2014-08-05T19:04:01.800 回答
0

不是 Youtube API——但您可以使用 Google Suggest API。调用此 URL:

http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&q=QUERY

这将返回您的应用可以解析和显示的建议术语的 json 响应。如果您更喜欢 XML 而不是 json,请更改

client=youtube

output=toolbar

(其余参数保持不变)。

于 2012-11-26T05:41:57.603 回答