我为其余的Response创建了一个映射,并将其命名为Data.A在通过RKObjectManager进行其余调用后,它没有加载对象.相反,它正在执行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
Run Code Online (Sandbox Code Playgroud)
我得到的错误消息如下
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.)"
Run Code Online (Sandbox Code Playgroud)
请帮忙!
校正后我改变了功能
-(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");
}
Run Code Online (Sandbox Code Playgroud)
这是正确的吗?因为现在对象似乎被加载但我得到的索引0超出了一个空数组的界限.我做错了吗?
您的 API 返回 HTTP 错误 401 未经授权。您的后端是否需要 HTTP 身份验证?如果是这样,请向以下人员提供正确的凭据RKClient:
[[RKClient sharedClient] setUsername:myUsername];
[[RKClient sharedClient] setPassword:myPassword];
Run Code Online (Sandbox Code Playgroud)
编辑:
我相信您在设置 RestKit 时遇到一些基本问题。考虑以下示例。
//in your appdelegate
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:8080/activiti-rest/service"];
[[RKClient sharedClient] setUsername:@"kermit"];
[RKClient sharedClient] setPassword:@"kermit"];
// don't forget to create your mapping here
RKObjectMapping *dataMapping = [RKObjectMapping mappingForClass:[Data class]];
[dataMapping mapKeyPath:@"myKeyPath" toAttribute:@"myAttr"];
[[manager mappingProvider] addObjectMapping: dataMapping];
Run Code Online (Sandbox Code Playgroud)
那么,你就可以这样做。
-(void)loadData{
// fetch your mapping
[RKObjectMapping *mapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[Data class]];
//request data
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/process-definitions?start=0&size=10&sort=id&order=asc" objectMapping:mapping delegate:self];
}
Run Code Online (Sandbox Code Playgroud)
首先,您需要进行设置(RKClient、映射和RKObjectManager)——只需执行一次。它们是单例,因此保留设置。我发现执行此操作的最佳位置是 AppDelegate - 请随意尝试,但请务必在执行任何请求之前进行设置。
当您要执行任何请求时,只需使用您的[[RKObjectManager sharedManager]单例来加载实际对象。
另外,我建议您阅读一些文档,例如。对象映射指南