我创建了一个针对REST响应的映射,并将其命名为“Data”。通过RKObjectManager进行REST调用后,它无法加载对象。相反,它执行了RKObjetLoader的didFailWithError方法。我的实现类继承自RKObjectLoaderDelegate。
@implementation RKObjectLoaderExamples
-(void)loadData{
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[Data class]];
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:8080/activiti-rest/service"];
[manager loadObjectsAtResourcePath:@"/process-definitions?start=0&size=10&sort=id&order=asc&username=kermit&password=kermit" objectMapping:mapping delegate:self] ;
NSLog(@"Loaded Data");
}
// RKObjectLoaderDelegate methods
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(@"objectLoaded");
Data* data = [objects objectAtIndex:0];
NSLog(@"Loaded Key: %@, Name: %@", data.key, data.name);
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSLog(@"Encountered an error: %@", error);
}
@end
我收到的错误信息如下:
2011-11-16 14:36:38.971 Views[16753:fb03] W restkit.network:RKResponse.m:182 Received an authentication challenge without any credentials to satisfy the request.
2011-11-16 14:36:38.974 Views[16753:fb03] W restkit.network:RKObjectLoader.m:242 Unable to find parser for MIME Type 'text/html'
2011-11-16 14:36:38.975 Views[16753:fb03] W restkit.network:RKObjectLoader.m:259 Encountered unexpected response with status code: 401 (MIME Type: text/html)
2011-11-16 14:36:38.976 Views[16753:fb03] Encountered an error: Error Domain=org.restkit.RestKit.ErrorDomain Code=4 "The operation couldn’t be completed. (org.restkit.RestKit.ErrorDomain error 4.)"
请帮帮我!
修改后,我将函数更改为:
-(void)loadData{
[RKClient setSharedClient:[[RKClient alloc] initWithBaseURL:@"http://localhost:8080/activiti-rest/service"]];
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[Data class]];
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:8080/activiti-rest/service"];
[manager setClient:[RKClient sharedClient]];
[[RKClient sharedClient] setUsername:@"kermit"];
[[RKClient sharedClient] setPassword:@"kermit"];
[manager loadObjectsAtResourcePath:@"/process-definitions?start=0&size=10&sort=id&order=asc" objectMapping:mapping delegate:self] ;
NSLog(@"Loaded Data");
}
这样做正确吗?因为现在对象似乎已经加载,但是我得到了一个空数组的索引0超出范围的错误。我做错了吗?
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[Data class]];
因此,restkit不知道如何将API响应映射到Data类。因此,您的初始问题已解决(身份验证),但您仍需要定义映射。 - mja