【问题标题】:Youtube API auto-complete search not workingYoutube API 自动完成搜索不起作用
【发布时间】:2016-05-04 14:50:43
【问题描述】:

我正在使用下面的代码,但它对我不起作用并且总是得到空响应,但是当该 URL 害虫进入浏览器时,它会下载一个包含以下文本的 f.txt 文件

window.google.ac.h(["Y",[["young thug",0],["youtube",0],["youth troye sivan",0],["young dolph",0],["yo gotti",0],["yg",0],["yoga",0],["you 应该在这里 cole swindell",0],["yandere 模拟器",0],["young thug 最好的朋友",0]],{"k":1,"l":"1","q":"Fu65vJmwPDpRvrCvJ_hO3MqI15U"}])

   @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...

    }

}} 

【问题讨论】:

    标签: objective-c autocomplete search-suggestion


    【解决方案1】:

    解决这个问题这里有两件事要知道:

    在 iOS 9 中,默认情况下不支持 http://。您必须安全地进行通信(使用 https://)。如果需要,您可以在 Info.plist 中关闭此功能。 NSFileManager URL 必须是磁盘上文件的路径——也就是说,它们必须是文件 URL。你的不是;这是一个 http:// 网址。如果您的目标是下载文件然后将其复制到某处,请使用 NSURLSession 的下载任务。

    【讨论】:

      猜你喜欢
      • 2012-07-01
      • 2012-11-12
      • 2017-09-16
      • 1970-01-01
      • 2021-04-26
      • 2016-01-21
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多