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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8157707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:27:04  来源:igfitidea点击:

Error with loading objects from a Response(RestKit)

iosxcoderestkit

提问by Anusha Pachunuri

I created a mapping for the rest Response and named it Data.After making the rest call through RKObjectManager,it is not loading the objects.Instead it is executing the didFailWithError method of RKObjetLoader.My implementation class inherits from RKObjectLoaderDelegate.

我为rest Response 创建了一个映射并将其命名为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  

The error messages i am getting are as follows

我收到的错误消息如下

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.)"

Please help!

请帮忙!

After correction i changed the function as

更正后,我将功能更改为

-(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");

}

Is it correct?Because now object seems to be loaded but i am getting an index 0 beyond bounds for an empty array.Am i doing it wrong?

正确吗?因为现在对象似乎已加载,但我得到的索引 0 超出了空数组的界限。我做错了吗?

回答by mja

Your API returns HTTP Error 401 Unauthorized. Does your backend require a HTTP authentication? If so, supply the correct credentials to the RKClient:

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

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

edit:

编辑:

I believe you have some fundamental problems in setting up the RestKit. Consider the following example.

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

then, you can do just this.

那么,你就可以做到这一点。

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

Firstly, you need to do the setup (RKClient, mappings and RKObjectManager) - you do it just once. They are singletons, so the settings are kept. I found that best place to do this is the AppDelegate - feel free to experiment, but be sure to make the setup before you do any requests.

首先,您需要进行设置 ( RKClient, 映射和RKObjectManager) - 您只需进行一次。它们是单例,所以设置被保留。我发现执行此操作的最佳位置是 AppDelegate - 随意尝试,但请务必在执行任何请求之前进行设置。

When you are about to do any requests just use your [[RKObjectManager sharedManager]singleton to load the actual objects.

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

Also, i recommend you reading some documentation, eg. the Object Mapping guide

另外,我建议您阅读一些文档,例如。所述对象映射导