Error loading objects from response (RestKit)

I created a mapping for the rest of the Response and named it Data.After making the rest of the call through RKObjectManager, it does not load objects. Instead, it executes the didFailWithError method for RKObjetLoader.My the 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 receive 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 the object is loading, but I get index 0 out of bounds for an empty array. Am I doing it wrong?

+6
source share
1 answer

Your API returns HTTP Error 401 Unauthorized. Does your server require HTTP authentication? If so, put the correct credentials in RKClient :

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

change

I believe that you have some fundamental problems when setting up 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 that.

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

First, you need to do the setup ( RKClient , matching, and RKObjectManager ) - you do this only once. They are single, so the settings are saved. I found that the best place to do this is AppDelegate - feel free to experiment, but be sure to tweak before making any queries.

When you are going to make any requests, just use the single [[RKObjectManager sharedManager] tag to load real objects.

In addition, I recommend that you read some documentation, for example. Object Mapping Guide

+3
source

Source: https://habr.com/ru/post/901651/


All Articles