实现LastFM API到IOS应用程序

时间:2015-06-28 21:31:51

标签: objective-c web-services

我有一个IOS应用程序,我试图实现LastFM API来搜索曲目名称,但我不知道如何准确地执行此操作。以下教程使用腐烂的西红柿作为例子,我运行良好。但是,当我尝试使用LastFM时,它不起作用。有解决方案吗我正在尝试使用的API链接是

http://ws.audioscrobbler.com/2.0/?method=track.search&track=Believe&api_key=f3f936c8534e7cad4fff55861704cbf0&format=json

我的代码示例

#import "SongSearchService.h"

@implementation SongSearchService

@synthesize searchTerm;
@synthesize delegate;
@synthesize results;

-(void)main {

    NSLog(@"Service has run");
    NSString *api_key = @"f3f936c8534e7cad4fff55861704cbf0";
    NSString *search_term = [searchTerm stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSString *url = [NSString stringWithFormat:@"http://ws.audioscrobbler.com/2.0/?method=track.search&track=Believe", api_key, search_term];

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];

    if (responseData !=nil) {
        NSError *error =nil;
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
                                                             options:kNilOptions error:&error];
        if (error) {
            [delegate serviceFinished:self withError:YES];
        } else {
            results = (NSArray *) [json valueForKey:@"Songs"];
            [delegate serviceFinished:self withError:NO];
        }
    } else {
        [delegate serviceFinished:self withError:YES];
    }
}
@end

0 个答案:

没有答案