0

我正在构建一个需要从 Foursquare 实现搜索结果的 iOS 应用程序。我正在使用 RestKit,但一直遇到相同的错误:“没有响应描述符与加载的响应匹配”

这是我的代码的相关部分:

    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://api.foursquare.com/v2"]];
objectManager.managedObjectStore = managedObjectStore;

[RKObjectManager setSharedManager:objectManager];

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Venue" inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{
 @"name":         @"name",
 @"id":           @"id",
 @"canonicalUrl": @"canonicalUrl"}];
entityMapping.identificationAttributes = @[@"id"];

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

[objectManager addResponseDescriptor:responseDescriptor];

//Create strings for auth
clientID = [NSString stringWithUTF8String:kCLIENTID];
clientSecret = [NSString stringWithUTF8String:kCLIENTSECRET];

[self loadVenues];

loadVenues 方法:

    NSString *currentLatLon = [NSString stringWithFormat:@"%1$f,%2$f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
NSDictionary *queryParams = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"20130601",@"v",
                             currentLatLon,@"ll",
                             clientID,@"client_id",
                             clientSecret,@"client_secret", nil];
[[RKObjectManager sharedManager]
getObjectsAtPath:@"https://api.foursquare.com/v2/venues/search"
parameters:queryParams
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    //Do something
    }
failure:^(RKObjectRequestOperation *operation, NSError *error) {
    //Do something else
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}];

核心数据模型: 在此处输入图像描述 在此处输入图像描述

这是我试图解析的那种 JSON 数据:

representation: {
meta =     {
    code = 200;
};
response =     {
    venues =         (
                    {
            canonicalUrl = "https://foursquare.com/v/union-square/40bbc700f964a520b1001fe3";
            categories =                 (
                                    {
                    icon =                         {
                        prefix = "https://foursquare.com/img/categories_v2/parks_outdoors/plaza_";
                        suffix = ".png";
                    };
                    id = 4bf58dd8d48988d164941735;
                    name = Plaza;
                    pluralName = Plazas;
                    primary = 1;
                    shortName = "Plaza / Square";
                }
            );
            contact =                 {
                formattedPhone = "(415) 781-7880";
                phone = 4157817880;
                twitter = unionsquaresf;
            };
            hereNow =                 {
                count = 8;
                groups =                     (
                                            {
                        count = 8;
                        items =                             (
                        );
                        name = "Other people here";
                        type = others;
                    }
                );
            };
            id = 40bbc700f964a520b1001fe3;
            location =                 {
                address = "Union Square Park";
                cc = US;
                city = "San Francisco";
                country = "United States";
                crossStreet = "btwn Post, Stockton, Geary & Powell St.";
                distance = 68;
                lat = "37.787750172585";
                lng = "-122.4076282253645";
                postalCode = 94108;
                state = CA;
            };
            name = "Union Square";
            referralId = "v-1370991275";
            restricted = 1;
            specials =                 {
                count = 0;
                items =                     (
                );
            };
            stats =                 {
                checkinsCount = 73242;
                tipCount = 172;
                usersCount = 39007;
            };
            url = "http://visitunionsquaresf.com";
            venuePage =                 {
                id = 34303229;
            };
            verified = 1;
        },
4

2 回答 2

2

当您调用获取数据时,请使用相对路径,而不是完整的 URL:

[[RKObjectManager sharedManager] getObjectsAtPath:@"venues/search" ...

同样,您的响应描述符路径模式应该匹配:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor
                                        responseDescriptorWithMapping:entityMapping
                                        pathPattern:@"venues/search" ...
于 2013-06-12T08:41:40.890 回答
0

这是我在查看 Wain 的建议后得出的结论。

在运行时 RestKit 找不到我的 responseDescriptor。这是我为了让我的 responseDescriptor 被识别而更改的内容:

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

在更改它并确保我使用了正确的路径之后(总是仔细检查你的路径!),我注意到我的键没有正确映射。

问题是我在实体模型中将我的一个键命名为“id”。“id”是一个系统关键字,会导致一大堆时髦的编译器错误。

我只需要回到我的数据模型并调整我的属性。

    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Venue" inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{
 @"name":         @"name",
 @"id":           @"venueID",
 @"canonicalUrl": @"canonicalUrl"}];
entityMapping.identificationAttributes = @[@"venueID"];
于 2013-06-13T00:05:30.637 回答