RestKit 简单的 JSON,错误的 pathPAttern?
Posted
技术标签:
【中文标题】RestKit 简单的 JSON,错误的 pathPAttern?【英文标题】:RestKit simple JSON, bad pathPAttern? 【发布时间】:2015-07-15 08:40:41 【问题描述】:JSON:
http://krzysztofkurzawa.com/articles/ http://krzysztofkurzawa.com/articles/1代码:
NSURL *baseURL = [NSURL URLWithString:@"http://www.krzysztofkurzawa.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:@"articles" keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk2 = [RKResponseDescriptor responseDescriptorWithMapping:mapping2 pathPattern:@"articles/:id" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk2];
[[RKObjectManager sharedManager] getObjectsAtPath:@"articles" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
NSLog(@"%@", [mappingResult firstObject]);
failure:^(RKObjectRequestOperation *operation, NSError *error)
];
[[RKObjectManager sharedManager] getObjectsAtPath:@"articles/1" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
NSLog(@"%@", [mappingResult firstObject]);
failure:^(RKObjectRequestOperation *operation, NSError *error)
];
错误:在关键路径中找不到可映射的对象表示 已搜索。", keyPath=null, NSLocalizedDescription=无响应 描述符与加载的响应匹配。
我没有任何想法。当 pathPattern 设置为 nil
时,第一个映射有效。为什么一定是nil
?也许它是创建休息路径模式所需要的。
【问题讨论】:
为什么在获取对象时使用 [RKObjectManager sharedManager]?我认为你应该更好地使用 objectManager 这是单例 - [RKObjectManager sharedManager] 但是您正在使用在第一行中创建的另一个 RKObjectManager 实例初始化映射 RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL]; 是的,你是对的。我将 [RKObjectManager sharedManager] 更改为 objectManager。要使用 [RKObjectManager sharedManager] 需要 [RKObjectManager setManager:]。但没有结果。错误“没有可映射的...” 当您尝试加载单个文章时,您的错误出现在第二个请求中?抱歉,我无法运行您的代码,因为 RestKit 在通过 Cocoapods 安装时会出现编译器错误 【参考方案1】:您的路径不正确,此外,在运行您的代码时,我不得不使用以下方法:
+ (instancetype)responseDescriptorWithMapping:(RKMapping *)mapping
method:(RKRequestMethod)method
pathPattern:(NSString *)pathPattern
keyPath:(NSString *)keyPath
statusCodes:(NSIndexSet *)statusCodes
您正在使用它的那个说它已被弃用。我猜你可能使用的是旧版本的 restkit。
这是有效的代码:
NSURL *baseURL = [NSURL URLWithString:@"http://www.krzysztofkurzawa.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodGET pathPattern:@"/articles/" keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping2 addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor *rk2 = [RKResponseDescriptor responseDescriptorWithMapping:mapping2 method:RKRequestMethodGET pathPattern:@"/articles/:id/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:rk2];
[[RKObjectManager sharedManager] getObjectsAtPath:@"/articles/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
NSLog(@"%@", [mappingResult firstObject]);
failure:^(RKObjectRequestOperation *operation, NSError *error)
];
[[RKObjectManager sharedManager] getObjectsAtPath:@"/articles/1/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
NSLog(@"%@", [mappingResult firstObject]);
failure:^(RKObjectRequestOperation *operation, NSError *error)
];
【讨论】:
【参考方案2】:这可能会阻止你
NSString *pathNotation = @"/Notations/:notation_id";
RKRoute *notationRoute = [RKRoute routeWithClass:[Notation class] pathPattern:pathNotation method:RKRequestMethodGET];
[self.objectManager.router.routeSet addRoute:notationRoute];
RKResponseDescriptor *notationMappingDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:notationMapping method:RKRequestMethodGET pathPattern:pathNotation keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
和 GET:
[[RKObjectManager sharedManager] getObject:nil path:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
//do something
failure:^(RKObjectRequestOperation *operation, NSError *error)
//do something
];
【讨论】:
以上是关于RestKit 简单的 JSON,错误的 pathPAttern?的主要内容,如果未能解决你的问题,请参考以下文章
RestKit 0.20.0 - 带有 JSON HTTP 正文请求的 getObjectsAtPath
Restkit JSON 错误解析 1969/12/07 到 1970/01/25 GMT 范围内的日期
Restkit 0.22.0 和 iOS 7 RKObjectManager postObject 不生成 JSON 数据