Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with loading objects from a Response(RestKit)

Tags:

xcode

ios

restkit

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.

@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?

like image 979
Anusha Pachunuri Avatar asked Nov 16 '11 19:11

Anusha Pachunuri


1 Answers

Your API returns HTTP Error 401 Unauthorized. Does your backend require a HTTP authentication? If so, supply the correct credentials to the 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.

//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.

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

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

like image 124
mja Avatar answered Oct 04 '22 19:10

mja