简体   繁体   English

iOS json无法从iTunes api检索数据

[英]iOS json not retrieving data from iTunes api

I am using the following code to try to get the trackName from iTunes API. 我正在使用以下代码尝试从iTunes API获取trackName。 It should come from this link for example: https://itunes.apple.com/search?term=EMINEM&entity=song&limit=3 It keeps returning nothing (label is blank and it is not because the label is too small) 例如,它应该来自此链接: https : //itunes.apple.com/search?term=EMINEM&entity=song&limit=3它始终不返回任何内容(标签为空,不是因为标签太小)

- (void)pressSearchKey {
    NSInteger numberOfResults = 3;
    NSString *searchString = self.keyboard.textField.text;

    NSString *encodedSearchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *finalSearchString = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?term=%@&entity=song&limit=%ld",searchString,numberOfResults];

    NSURL *searchURL = [NSURL URLWithString:finalSearchString];
    dispatch_queue_t iTunesQueryQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(iTunesQueryQueue, ^{
        NSError *error = nil;
        NSData *data = [[NSData alloc] initWithContentsOfURL:searchURL options:NSDataReadingUncached error:&error];

        if (data && !error) {
            NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

            NSArray *array = [JSON objectForKey:@"results"];
            NSArray *arrayTracks;
            for (NSDictionary *bpDictionary in array) {
                arrayTracks = [bpDictionary objectForKey:@"trackName"];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                self.keyboard.firstLabel.text = [arrayTracks objectAtIndex:1];
            });
        }
    });
}

You've got a couple of problems in this one too. 您在这个问题中也遇到了一些问题。

  1. You're never utilizing your encodedSearchString when building finalSearchString 你永远不会利用你的encodedSearchString建设时finalSearchString
  2. Your example URL calls https://itunes.apple.com/ search whereas the URL in your code is https://itunes.apple.com/ lookup . 您的示例URL调用https://itunes.apple.com/ search,而代码中的URL为https://itunes.apple.com/ lookup Changing it to search returns the expected results. 将其更改为搜索将返回预期结果。

Fixed: 固定:

NSInteger numberOfResults = 3;
NSString *searchString = @"EMINEM";

NSString *encodedSearchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *finalSearchString = [NSString stringWithFormat:@"https://itunes.apple.com/search?term=%@&entity=song&limit=%ld",encodedSearchString,numberOfResults];

NSURL *searchURL = [NSURL URLWithString:finalSearchString];
dispatch_queue_t iTunesQueryQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(iTunesQueryQueue, ^{
    NSError *error = nil;
    NSData *data = [[NSData alloc] initWithContentsOfURL:searchURL options:NSDataReadingUncached error:&error];

    if (data && !error) {
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSArray *array = [JSON objectForKey:@"results"];
        NSArray *arrayTracks;
        for (NSDictionary *bpDictionary in array) {
            arrayTracks = [bpDictionary objectForKey:@"trackName"];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"%@", [arrayTracks objectAtIndex:1]);
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM