从响应中加载对象时出错(RestKit)

时间:2011-11-16 19:46:36

标签: ios xcode restkit

我为其余的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  

我得到的错误信息如下

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超出了空数组的界限。我做错了吗?

1 个答案:

答案 0 :(得分:3)

您的API返回HTTP错误401未经授权。您的后端是否需要HTTP身份验证?如果是,请向RKClient提供正确的凭据:

[[RKClient sharedClient] setUsername:myUsername];
[[RKClient sharedClient] setPassword:myPassword];

修改

我相信你在设置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];

然后,你可以这样做。

 -(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];
}

首先,您需要进行设置(RKClient,映射和RKObjectManager) - 您只需一次。他们是单身人士,因此保留了设置。我发现做到这一点的最佳位置是AppDelegate - 随意进行试验,但一定要在做任何请求之前进行设置。

当您要做任何请求时,只需使用[[RKObjectManager sharedManager]单例来加载实际对象。

另外,我建议你阅读一些文档,例如。 Object Mapping guide