Question

Im trying to send a JSON to a local Wordpress server, the mapping I have is the one below:

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];

RKObjectMapping *orderEntryMapping = [RKObjectMapping requestMapping];

[orderEntryMapping addAttributeMappingsFromDictionary:@{
                                                        @"title": @"title"
                                                        }];

RKRequestDescriptor *requestDescriptorOrderEntry = [RKRequestDescriptor  requestDescriptorWithMapping:orderEntryMapping objectClass:[OrderSend class]   rootKeyPath:@"posts" method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptorOrderEntry];

The OrderSend class is the one below:

#import <Foundation/Foundation.h>

@interface OrderSend : NSObject

@property (nonatomic) NSString *title;

@end

and this is the method that is sending the request:

-(void) submitOrder:(OrderSend *) order completionHandler:(ResultObjectHandler) completionBlock
{

RKObjectManager *objectManager = [RKObjectManager sharedManager];

NSDictionary *parameters = @{
               @"json" : @"posts.create_post"
               };


[objectManager postObject:order path:@"" parameters:parameters success:^(RKObjectRequestOperation *operation, RKMappingResult *result)
 {
     NSLog(@"We object mapped the response with the following result: %@", result);
     completionBlock(result);
 }
                  failure:^(RKObjectRequestOperation *operation, NSError *error)
 {
     [self handleFailure:operation withError:error];
 }];    
 }

-(void) cancel{

[[RKObjectManager sharedManager].operationQueue cancelAllOperations];
}

-(void) handleFailure:(RKObjectRequestOperation *)operation withError:(NSError*)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                message:[error localizedDescription]
                                               delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];
NSLog(@"Hit error: %@", error);
}

Im using the JSON API for Wordpress, but Im getting this error:

{NSLocalizedDescription=No mappable object representations were found at the key paths     searched., NSLocalizedFailureReason=The mapping operation was unable to find any nested   object representations at the key paths searched: categories, posts
The representation inputted to the mapper was found to contain nested object representations at the following key paths: error, status
This likely indicates that you have misconfigured the key paths for your mappings.,   keyPath=null, DetailedErrors=(
)}

Can anyone help me please?, thanks in advance

The solution was to create a Response Descriptor like the code below:

RKResponseDescriptor *responseDescriptorOrderEntry = [RKResponseDescriptor     responseDescriptorWithMapping:orderEntryMapping method:RKRequestMethodAny pathPattern:nil     keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
 [objectManager addResponseDescriptor:responseDescriptorOrderEntry];
Était-ce utile?

La solution

You are defining a request descriptor, but you aren't defining a response descriptor. So, RestKit doesn't know what to do with the response.

You need to create a response descriptor with an associated mapping so that RestKit knows how to apply the response. By default RestKit will try to apply the response data to the source object (order), but it needs to know how to do that...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top