6

我正在尝试使用 RestKit 将 iTunes 搜索 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 的响应将内容类型标记为“文本/javascript”,而正文包含有效的 JSON。我已经尝试告诉 RKObjectRequestOperation text/javascript 是可接受的内容类型。

有人对 iTunes API 和 RestKit 有任何经验吗?我很好奇这是否是我的一个简单的映射问题,或者声称内容类型是 javascript 而它似乎是有效的 JSON 的 iTunes API 是否正在抛弃 RestKit。下面是在我的示例中记录的 iTunes 搜索 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"}]
    }
4

1 回答 1

2

有两件事可以完成这项工作。

你得到的第一个:

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

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

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

然后一切正常!

于 2013-06-30T06:08:05.347 回答