使用RESTKit映射iTunes Search API结果

时间:2012-10-21 05:52:50

标签: ios restkit itunes-search-api

我正在尝试使用RestKit将iTunes Search API结果加载到自定义对象中。我为结果定义了一个简单的NSObject子类:

@interface LSiTunesResult : NSObject

@property (nonatomic) NSInteger itemID;
@property (nonatomic) NSInteger artistID;

@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *artistName;
@property (strong, nonatomic) NSString *collectionName;
@property (strong, nonatomic) NSString *itemDescription;

@property (strong, nonatomic) NSURL *imageURL;
@property (strong, nonatomic) NSURL *thumbnailURL;
@property (strong, nonatomic) NSURL *trackPreviewURL;
@property (strong, nonatomic) NSURL *trackViewURL;

//
+ (RKObjectMapping *)objectMapping;

@end

为简单起见,我已经定义了对象映射

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[LSiTunesResult class]]; [mapping addAttributeMappingsFromDictionary:@{ @"artistName" : @"artistName" }];

我尝试了几种不同的方法来实际请求结果,同时使用自定义RKObjectManager和RKObjectRequestOperation。我使用RKObjectRequestOperation的简单示例如下所示:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[LSiTunesResult objectMapping] pathPattern:nil keyPath:@"results" statusCodes:nil];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:urlRequest responseDescriptors:@[responseDescriptor]];
[operation.HTTPRequestOperation setAcceptableContentTypes:[NSSet setWithObject:@"text/javascript"]];

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"The search results are: %@", [mappingResult array]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failure reason: %@", [error localizedDescription]);
}];

[operation start];

不幸的是,我从RKResponseMapperOperation收到以下错误:“restkit.network:RKResponseMapperOperation.m:197无法解析响应数据:加载了内容类型为”text / javascript“的无法处理的响应(200)

我有RestKit网络登录,似乎来自iTunes API的响应将内容类型标记为“text / javascript”,而正文包含有效的JSON。我已经尝试告诉RKObjectRequestOperation text / javascript是可接受的内容类型。

有没有人对iTunes API和RestKit有任何经验?我很好奇,如果这是一个简单的映射问题,或者如果iTunes API声称内容类型是javascript,而它似乎是有效的JSON正在抛弃RestKit。以下是我的示例中记录的iTunes Search API响应示例。

   2012-10-21 01:48:54.061 Listen[99898:617] T restkit.network:RKHTTPRequestOperation.m:124 GET 'http://itunes.apple.com/search?term=The%20Lumineers&country=US&media=music&limit=1' (200):
    response.headers={
        "Cache-Control" = "no-transform, max-age=60";
        Connection = "keep-alive";
        "Content-Encoding" = gzip;
        "Content-Length" = 519;
        "Content-Type" = "text/javascript; charset=utf-8";
        Date = "Sun, 21 Oct 2012 05:48:54 GMT";
        Vary = "Accept-Encoding";
        "X-Apple-Partner" = "origin.0";
        "apple-timing-app" = "49 ms";
        "x-apple-application-instance" = 1018018;
        "x-apple-application-site" = NWK;
        "x-apple-orig-url-path" = "/search?term=The%20Lumineers&country=US&media=music&limit=1";
        "x-apple-translated-wo-url" = "/WebObjects/MZStoreServices.woa/ws/wsSearch?term=The%20Lumineers&country=US&media=music&limit=1";
        "x-webobjects-loadaverage" = 0;
    }
    response.body=


    {
     "resultCount":1,
     "results": [
    {"wrapperType":"track", "kind":"music-video", "artistId":350720227, "trackId":516035614, "artistName":"The Lumineers", "trackName":"Ho Hey", "trackCensoredName":"Ho Hey", "artistViewUrl":"https://itunes.apple.com/us/artist/the-lumineers/id350720227?uo=4", "trackViewUrl":"https://itunes.apple.com/us/music-video/ho-hey/id516035614?uo=4", "previewUrl":"http://a404.v.phobos.apple.com/us/r30/Video/fd/81/e1/mzi.gxodlwda..640x256.h264lc.u.p.m4v", "artworkUrl30":"http://a1838.phobos.apple.com/us/r30/Video/v4/41/64/37/416437c2-c02e-4706-3f13-a47f50ec2ccc/Cover.40x30-75.jpg", "artworkUrl60":"http://a621.phobos.apple.com/us/r30/Video/v4/41/64/37/416437c2-c02e-4706-3f13-a47f50ec2ccc/Cover.80x60-75.jpg", "artworkUrl100":"http://a1629.phobos.apple.com/us/r30/Video/v4/41/64/37/416437c2-c02e-4706-3f13-a47f50ec2ccc/Cover.100x100-75.jpg", "collectionPrice":1.99, "trackPrice":1.99, "releaseDate":"2012-04-03T07:00:00Z", "collectionExplicitness":"notExplicit", "trackExplicitness":"notExplicit", "trackTimeMillis":160952.0, "country":"USA", "currency":"USD", "primaryGenreName":"Singer/Songwriter"}]
    }

1 个答案:

答案 0 :(得分:2)

使这项工作有两件事。

你得到的第一个:

operation.HTTPRequestOperation.acceptableContentTypes = [NSSet setWithObject:@"text/javascript"];

第二个是为这种类型注册json序列化器。

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/javascript"];

然后一切正常!