使用 RESTKit 映射 iTunes 搜索 API 结果

Posted

技术标签:

【中文标题】使用 RESTKit 映射 iTunes 搜索 API 结果【英文标题】:Mapping iTunes Search API results with RESTKit 【发布时间】:2012-10-21 05:52:50 【问题描述】:

我正在尝试使用 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"]
    

【问题讨论】:

【参考方案1】:

要实现这一点,需要做两件事。

你得到的第一个:

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

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

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

然后一切正常!

【讨论】:

以上是关于使用 RESTKit 映射 iTunes 搜索 API 结果的主要内容,如果未能解决你的问题,请参考以下文章

RESTKit 异常:在搜索的关键路径中未找到可映射的对象表示

使用 RESTKit 将 url 参数映射到对象

IOS - RESTKIT - 以多种方式映射结果

RestKit 不喜欢为 POST 响应映射不同的对象

如何使用 RestKit 测试这个对象映射

使用restkit api目标c调用Web服务时出错