【问题标题】:Youtube API auto-complete searchYoutube API 自动完成搜索
【发布时间】:2012-11-12 18:23:06
【问题描述】:

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

【问题讨论】:

    标签: iphone objective-c search autocomplete youtube-api


    【解决方案1】:

    不是 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
    

    (其余参数保持不变)。

    【讨论】:

    • 其中 q=QUERY 是您要搜索的单词?++
    • 是的,q=QUERY 是您的搜索查询。
    【解决方案2】:

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

    所以...我正在使用这个 API : http://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 或使用 异步块。 (只需将第一个方法的内容插入到 Async 块中...)

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

    玩得开心!!!

    【讨论】:

    猜你喜欢
    • 2012-07-01
    • 1970-01-01
    • 2016-01-21
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多