【问题标题】:Problems with RESTKit, cannot map properlyRESTKit 的问题,无法正确映射
【发布时间】:2013-01-02 11:29:29
【问题描述】:

我正在学习 RESTKit,我正在关注this 教程,这是根据之前版本的 RESTKit,并且在 v0.20.0 中发生了很多变化,所以我正在阅读新版本并执行相同的任务在教程中。

现在映射出现问题,我花了好几个小时都没有成功。我得到的错误是:

error: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=0x758d320 {NSErrorFailingURLStringKey=https://api.foursquare.com/v2/venues/search?client_id=<client id>&client_secret=KYHQHRYJ4PYFJYEG1F1IQVLQDM4G1HZ1STB2O1VUVUVQZBGS<client secret>&ll=37.33,-122.03&query=coffee&v=20120602, NSLocalizedFailureReason=A 200 response was loaded from the URL 'https://api.foursquare.com/v2/venues/search?client_id=<client id>&client_secret=<client secret>&ll=37.33,-122.03&query=coffee&v=20120602', which failed to match all (1) response descriptors:
  <RKResponseDescriptor: 0x9566a60 baseURL=https://api.Foursquare.com/v2/ pathPattern=(null) statusCodes=200-299> failed to match: response URL 'https://api.foursquare.com/v2/venues/search?client_id=<client id>&client_secret=<client secret>&ll=37.33,-122.03&query=coffee&v=20120602' is not relative to the baseURL 'https://api.Foursquare.com/v2/'., NSLocalizedDescription=No response descriptors match the response loaded., keyPath=null, NSErrorFailingURLKey=https://api.foursquare.com/v2/venues/search?client_id=<client id>&client_secret=<client secret>&ll=37.33,-122.03&query=coffee&v=20120602, NSUnderlyingError=0x758d3a0 "No mappable object representations were found at the key paths searched."}

Venue.h 是:

#import <Foundation/Foundation.h>

@interface Venue : NSObject

@property (strong, nonatomic) NSString *name;

@end

Venue.m 是:

#import "Venue.h"

@implementation Venue
@synthesize name;

@end

我在 MasterViewController.m 中的 ViewDidAppear 方法中做所有事情,即:

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSURL *baseURL = [NSURL URLWithString:@"https://api.Foursquare.com/v2"];

    AFHTTPClient * client = [AFHTTPClient clientWithBaseURL:baseURL];
    [client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];

    RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

    RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Venue class]];
    [venueMapping addAttributeMappingsFromDictionary:@{
     @"name" : @"name"
     }];

    RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:venueMapping
                                                                                        pathPattern:nil
                                                                                            keyPath:@"response.venues"
                                                                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [objectManager addResponseDescriptor:responseDescriptor];

    NSString *latLon = @"37.33,-122.03";
    NSString *clientID = [NSString stringWithUTF8String:kCLIENTID];
    NSString *clientSecret = [NSString stringWithUTF8String:kCLIENTSECRET];

    NSDictionary *queryParams;
    queryParams = [NSDictionary dictionaryWithObjectsAndKeys:latLon, @"ll", clientID, @"client_id", clientSecret, @"client_secret", @"coffee", @"query", @"20120602", @"v", nil];
    //NSLog(@"object manager: %@ - %@", objectManager.baseURL, [[objectManager responseDescriptors] objectAtIndex:0]);

    /*
    RKRelationshipMapping* relationShipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"venues"
                                                                                             toKeyPath:@"venues"
                                                                                           withMapping:venueMapping];
    [venueMapping addPropertyMapping:relationShipMapping]; */
    [objectManager getObjectsAtPath:@"https://api.Foursquare.com/v2/venues/search"
                         parameters:queryParams
                         success:^(RKObjectRequestOperation * operaton, RKMappingResult *mappingResult)
                         {
                             NSLog(@"success: mappings: %@", mappingResult);
                         }
                         failure:^(RKObjectRequestOperation * operaton, NSError * error)
                         {
                             NSLog (@"failure: operation: %@ \n\nerror: %@", operaton, error);
                         }];

}

我检查了浏览器中的响应,一切正常,但由于某种原因,我所做的一切都没有消除错误。我卡在映射的第一步,无法前进。互联网上有关该主题的所有示例和文档都已过时,并且对 RESTKit 的旧版本没有经验,我无法弄清楚。请告诉我我做错了什么。

使用 xcode 4.5 并为 iOS6 构建。

响应是(在浏览器上检查):

{"meta":{"code":200},"response":{"venues":[{"id":"4f482132e4b0f85d07c34cb5","name":"De Anza 3 Coffee Bar","contact":{},"location":{"address":"10500 De Anza Blvd","lat":37.329438,"lng":-122.03083,"distance":96,"postalCode":"95014","city":"Cupertino","state":"CA","country":"United States","cc":"US"},"canonicalUrl":"https:\/\/foursquare.com\/v\/de-anza-3-coffee-bar\/4f482132e4b0f85d07c34cb5","categories":[{"id":"4bf58dd8d48988d1e0931735","name":"Coffee Shop","pluralName":"Coffee Shops","shortName":"Coffee Shop","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories\/food\/coffeeshop_","sizes":[32,44,64,88,256],"name":".png"},"primary":true}],"verified":false,"restricted":true,"stats":{"checkinsCount":1,"usersCount":1,"tipCount":0},"likes":{"count":0,"groups":[]},"specials":{"count":0,"items":[]},"hereNow":{"count":0,"groups":[]},"referralId":"v-1357121711"},{"id":"40ccea80f964a52019011fe3","name":"Coffee Society","contact":{"phone":"4087258091","formattedPhone":"(408) 725-8091"},"location":{"address":"21265 Stevens Creek Blvd","crossStreet":"at Oaks Shopping Center","lat":37.32330342,"lng":-122.047382,"distance":1709,"postalCode":"95014","city":"Cupertino","state":"CA","country":"United States","cc":"US"},"canonicalUrl":"https:\/\/foursquare.com\/v\/coffee-society\/40ccea80f964a52019011fe3","categories":[{"id":"4bf58dd8d48988d1e0931735","name":"Coffee Shop","pluralName":"Coffee Shops","shortName":"Coffee Shop","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories\/food\/coffeeshop_","sizes":[32,44,64,88,256],"name":".png"},"primary":true}],"verified":false,"restricted":true,"stats":{"checkinsCount":2536,"usersCount":869,"tipCount":24},"likes":{"count":0,"groups":[]},"specials":{"count":0,"items":[]},"hereNow":{"count":0,"groups":[]},"referralId":"v-1357121711"}, .....

【问题讨论】:

    标签: iphone ios restkit


    【解决方案1】:

    我尝试将完全相同的教程转换为 0.20 版,最后为我解决的问题是将 URL 设为小写 (https://api.Foursquare.com/v2)。用小写 f 替换那个大写的“F”,所以都是小写的。 RESTKit 似乎需要全部小写并且区分大小写,但我是 iOS 编程和 RESTKit 的新手。

    另请注意:

    1. 不需要 RKRelationshipMapping 的东西
    2. 路径模式:无 & keyPath:@"response.venues"

    我相信你已经有了这些,但我想详细一点以防万一。

    【讨论】:

    • 非常感谢,我没想到会是这个问题,也感谢您将教程转换为 v0.20 的努力!
    • @Tilo 我们在哪里可以找到您转换后的教程?会有很大帮助的。
    【解决方案2】:

    对于来到这里但没有 CAPS 问题的人(比如我),请查看 RestKit 的创建者 this github resolution 的帖子。它帮助我弄清楚了my path issues

    【讨论】:

      【解决方案3】:

      您是否尝试过将 keyPath 简单地称为“venues”而不是“response.venues”?

      【讨论】:

        【解决方案4】:

        由于您在自定义 HTTP 客户端中使用了 baseURL,因此请尝试仅使用路径扩展名:

         [objectManager getObjectsAtPath:@"/venues/search"
                             parameters:queryParams
                             success:^(RKObjectRequestOperation * operaton, RKMappingResult *mappingResult)
                             {
                                 NSLog(@"success: mappings: %@", mappingResult);
                             }
                             failure:^(RKObjectRequestOperation * operaton, NSError * error)
                             {
                                 NSLog (@"failure: operation: %@ \n\nerror: %@", operaton, error);
                             }];
        

        并在响应描述符中添加路径:

        RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:venueMapping
                                                                                            pathPattern:@"/venues/search"
                                                                                                keyPath:@"venues"
                                                                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
        

        【讨论】:

        • 也试过了,没有成功。我已经构建了这个和其他几个可以在旧版本的 RestKit (0.10.x) 上正常工作的示例,但由于某种原因,我无法在最新版本上正常工作。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-22
        • 1970-01-01
        • 2012-04-30
        • 1970-01-01
        相关资源
        最近更新 更多