1

I want to get this JSON data with my iOS app: https://sleepy-journey-2871.herokuapp.com/users.json .... RestKit tries to get these users from the url, but it returns 0 objects and says "No mappable representations were found at the key paths searched. No response descriptors match the response loaded."

PLEASE help me figure out what I'm missing or doing wrong! I've been battling this for weeks.

I have Xcode 5.0.2, I successfully installed RestKit with Cocoapods and a Podfile that looks like this:

platform :ios, '6.0'
pod 'RestKit', '~> 0.22.0'
pod 'RestKit/Testing'
pod 'RestKit/Search'

My AppDelegate.m file is below (the didFinishLaunchingWithOptions method):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    // Override point for customization after application launch. 

    //Set base url 
    NSString *baseUrl = @"https://sleepy-journey-2871.herokuapp.com"; 

    //initialize the the http client with baseUrl 
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseUrl]]; 

    //initialize the RKObjectManager with our http client 
    RKObjectManager *manager = [[RKObjectManager alloc] initWithHTTPClient:httpClient]; 

    //add text/plain as a JSON content type to properly parse errors 
    [RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"]; 

    //register JSONRequestOperation to parse JSON in requests 
    [manager.HTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 

    //state that we are accepting JSON content type 
    [manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON]; 

    //configure so that we want the outgoing objects to be serialized into JSON 
    manager.requestSerializationMIMEType = RKMIMETypeJSON; 

    //set the shared instance of the object manager, so that we can easily re-use it later 
    [RKObjectManager setSharedManager:manager];     

    return YES;
}

I have this code in the viewDidLoad method of the view controller that first loads when the app is launched:

- (void)viewDidLoad
{
    [super viewDidLoad];
    RKObjectManager *manager = [RKObjectManager sharedManager];
    [manager getObjectsAtPath:@"/users"
                   parameters:nil
                      success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
    {
        NSLog(@"Loaded databases: %@", [mappingResult array]);
    }
                      failure:^(RKObjectRequestOperation *operation, NSError *error)
    {
        NSLog(@"Error: %@", [error localizedDescription]);
    }];
 // Do any additional setup after loading the view, typically from a nib.
}

1 Answer 1

0

You have to:

  1. Create a User class.
  2. Create a mapping for User class.
  3. Create a response descriptor.

Pseudocode could look something like:

@interface User : NSObject
@property (nonatomic, copy) NSNumber *userID;
@property (nonatomic, copy) NSString *name;
...
@end

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]];
[mapping addAttributeMappingsFromDictionary:@{
                                              @"name":   @"name",
                                              @"id":     @"userID"
                                              ...
                                              }];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                        method:RKRequestMethodAny
                                                                                   pathPattern:nil
                                                                                       keyPath:nil
                                                                                   statusCodes:nil];
...
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request
                                                                    responseDescriptors:@[responseDescriptor]];

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    NSLog(@"The public timeline Tweets: %@", [result array]);
} failure:nil];

[operation start];

Take a look at examples here.

1
  • So does the 'RKObjectRequestOperation *operation ...' part and the parts below it still go within that User class? When will the '[operation start]' code run?
    – EAB
    Apr 1, 2014 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.