2014-04-20 43 views
2

我是RestKit和Objective C的新手。我试图从带有RestKit的Web服务中检索数据。我已经完成了映射,但一些我得到的错误,它无法找到匹配的响应。由于JSON格式没有密钥,因此我使用的是没有KVC的映射。Restkit映射 - 没有响应描述符匹配加载的响应

这里是映射实现

+(RKObjectMapping *)venueMapping { 
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[venue class]]; 
    NSDictionary *mappingDictionary = @{@"name":@"name"}; 
    [mapping addAttributeMappingsFromDictionary:mappingDictionary]; 

    return mapping; 
} 

我只有一个值,想看看是否有任何匹配。下面是回复描述符

-(void) loadVenue:(void (^)(venue *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure{ 

    RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace); 
    [self getObjectsAtPath:@"venue" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){ 

    if(success){ 
     venue *venues = (venue *)[mappingResult.array firstObject]; 
     success(venues); 
    } 

    }failure:^(RKObjectRequestOperation *operation, NSError *error){ 
     if(failure){ 
      failure(operation,error); 
    } 
}]; 

} 

- (void) setupResponseDescriptors { 
[super setupResponseDescriptiors]; 

RKResponseDescriptor *authenticatedUserResponseDescriptors = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider venueMapping] method:RKRequestMethodGET pathPattern:@"venue" keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
[self addResponseDescriptor:authenticatedUserResponseDescriptors]; 
} 

我一直在阅读文档并尝试各种方法,但我仍然没有解决这个问题。任何人有任何想法?

附加是一个示例JSON返回。

(
    { 
    coverType = "<null>"; 
    description = ""; 
    name = "McDonald"; 
    priv = 1; 
    rates =(); 
    }, 
    { 
    coverType = "<null>"; 
    description = ""; 
    name = "My House"; 
    priv = 1; 
    rates =(); 
    }, 
) 

新增追踪日志

2014-04-22 21:45:55.636 App_Test[420:60b] I restkit:RKLog.m:34 RestKit logging initialized... 
2014-04-22 21:45:56.064 App_Test[420:60b] I restkit.network:RKObjectRequestOperation.m:180 GET 'http://api.kontakt.io/venue' 
2014-04-22 21:46:46.327 App_Test[420:f03] D restkit.object_mapping:RKMapperOperation.m:377 Executing mapping operation for representation: (
     { 
     beacons =   (
     ); 
     beaconsCount = 0; 
     coverType = "<null>"; 
     description = ""; 
     id = "212321"; 
     lat = "<null>"; 
     lng = "<null>"; 
     managers =   (
     ); 
     name = McDonald; 
     priv = 1; 
     rates =   (
     ); 
     users =   (
     ); 
    } 
) 
and targetObject: (null) 
2014-04-22 21:46:46.328 App_Test[420:f03] D restkit.object_mapping:RKMapperOperation.m:403 Finished performing object mapping. Results: (null) 
2014-04-22 21:46:46.328 App_Test[420:570b] E restkit.network:RKObjectRequestOperation.m:243 GET 'http://api.kontakt.io/venue' (200 OK/0 objects) [request=0.9645s mapping=0.0000s total=50.3422s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=0x9556ab0 {NSErrorFailingURLStringKey=http://api.kontakt.io/venue, NSLocalizedFailureReason=A 200 response was loaded from the URL 'http://api.kontakt.io/venue', which failed to match all (0) response descriptors:, NSLocalizedDescription=No response descriptors match the response loaded., keyPath=null, NSErrorFailingURLKey=http://api.kontakt.io/venue, NSUnderlyingError=0x9557c30 "No mappable object representations were found at the key paths searched."} 
+0

我试图把整个映射在代码中,这不是正确映射情况。但似乎它仍然无法正常工作。不过,我在调试期间发现了这个错误,** NSUnderlyingError:摘要字符串解析错误**。这是否意味着我的映射声明有一些问题? – greenhatman

+0

进一步调试后,似乎baseURL不匹配。不太确定发生了什么事。将尝试进一步调试 – greenhatman

+0

您能否为响应添加跟踪日志输出?并将响应描述符键路径更改为'nil'。 – Wain

回答

4

该错误消息表示:

which failed to match all (0) response descriptors

这表示(由于在该消息中的(0)),其不被称为所以没有响应描述符被添加到所述对象管理器的setupResponseDescriptors方法。

+0

中提到的路径模式谢谢@Wain!我在想它可能是映射是错误定义的或者是某些东西,但显然它不是在objectManager中由于命名不匹配而调用的。傻我。 – greenhatman

0

您正试图在路径获取对象“场地”,但似乎没有要你在你的JSON有一个“场地”对象的任何指示。

+0

不知道我是否正确阅读,但根据文档'许多Web API的返回他们的JSON没有任何可用于映射选择的嵌套属性。在这些情况下,你仍然可以使用RestKit,你只需要使用URL匹配。“这就是为什么我使用文档 – greenhatman

0

如果你不使用CoreData,确保Restkit在您Podfile由于数据不足,定义如下

pod 'RestKit/Network', '~> 0.23' pod 'RestKit/ObjectMapping', '~> 0.23'