我正在学习Rest-kit。我正在尝试解析此网址https://api.coursera.org/api/catalog.v1/courses?fields=language,shortDescription
我创建了一个Courses课程。
@interface Courses : NSObject
@property (nonatomic, strong) NSString *name;
@end
并在viewcontroller中我写了下面的代码
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureRestKit];
[self loadCourses];
}
- (void)configureRestKit
{
// https://api.coursera.org/api/catalog.v1/courses?fields=language,shortDescription
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:@"https://api.coursera.org"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
// setup object mappings
RKObjectMapping *courseMapping = [RKObjectMapping mappingForClass:[Courses class]];
[courseMapping addAttributeMappingsFromDictionary:@{
@"name":@"name"
}];
// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:courseMapping method:RKRequestMethodGET pathPattern:@"/api/catalog.v1/courses?fields=language,shortDescription" keyPath:@"elements" statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
}
- (void)loadCourses
{
[[RKObjectManager sharedManager] getObjectsAtPath:@"/api/catalog.v1/courses?fields=language,shortDescription" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"What do you mean by 'there is no Courses?': %@", error);
}];
}
错误:_Error Domain = org.restkit.RestKit.ErrorDomain Code = 1001"没有响应描述符与加载的响应匹配。" 我没有成功阻止回应。请指正。提前致谢
答案 0 :(得分:1)
不要将查询参数添加到响应描述符的路径模式中,并将它们设置为请求中的参数:
pathPattern:@"/api/catalog.v1/courses"
一般来说,我也会说/api/catalog.v1/
应该是基本网址的一部分,所以你会有:
NSURL *baseURL = [NSURL URLWithString:@"https://api.coursera.org/api/catalog.v1/"];
...
... pathPattern:@"courses" ...
...
... getObjectsAtPath:@"courses" parameters:@{ @"fields" : @"language,shortDescription" } ...