用于POST请求的iOS RestKit响应映射

时间:2014-03-11 09:06:07

标签: ios restkit restkit-0.20

在我的AppDelegate中,我正在创建这样的映射:

NSDictionary *basicResponseObjectMapping = @{
                                                 @"success" : @"Success",
                                                 @"error" : @"Error",
                                                 };

NSDictionary *reviewObjectMapping = @{
                                          @"text" : @"Text",
                                          };

[manager addResponseDescriptor:ratingResponseDescriptor];

RKObjectMapping *addRatingMapping = [RKObjectMapping mappingForClass:[BasicResponse class]];
[ratingMapping addAttributeMappingsFromDictionary:basicResponseObjectMapping];

RKResponseDescriptor *addRatingResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:addRatingMapping
                                                                                                 method:RKRequestMethodAny
                                                                                            pathPattern:@"/api/ratepoint"
                                                                                                keyPath:nil
                                                                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptor:addRatingResponseDescriptor];

RKObjectMapping *addRatingRequestMapping = [RKObjectMapping mappingForClass:[Review class]];
[addRatingRequestMapping addAttributeMappingsFromDictionary:reviewObjectMapping];
RKRequestDescriptor *addRatingRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[addRatingRequestMapping inverseMapping]
                                                                                        objectClass:[Review class]
                                                                                        rootKeyPath:nil
                                                                                             method:RKRequestMethodAny];
[manager addRequestDescriptor:addRatingRequestDescriptor];

这些是我的课程:

@interface BasicResponse : NSObject

@property(nonatomic) NSNumber *Success;
@property(nonatomic) NSString *Error;

@end

@interface Review : NSObject

@property(nonatomic) NSString *Text;

@end

我发送的请求是这样的:

RKObjectManager *manager = [RKObjectManager sharedManager];
[manager setRequestSerializationMIMEType:RKMIMETypeFormURLEncoded];
Review *review = [[Review alloc]init];
[review setText:reviewTextView.text];
[manager postObject:review
               path:[NSString stringWithFormat:@"/api/ratepoint/%@?pid=%@&stars=%@", sid, actualPlace.Id,[NSString stringWithFormat:@"%d", [ratingControl rating]]]
         parameters:nil
            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
 {
     NSArray* statuses = [mappingResult array];

     BasicResponse *basicResponse = statuses.firstObject;

 }
            failure:^(RKObjectRequestOperation *operation, NSError *error)
 {
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                     message:[error localizedDescription]
                                                    delegate:nil
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
     [alert show];
 }];

当我从RESTKit查看日志记录时,请求映射是可以的。但我对响应映射有问题。我想我一切都还好,但我得到了回应:

{"success":0,"error":"Some error"}

这应该由我的映射映射,但事实并非如此。这是log trace in pastebin

1 个答案:

答案 0 :(得分:0)

您的回复期望与@"/api/ratepoint"的路径匹配,但您实际拥有的是@"/api/ratepoint/...?...。所以它不匹配。这就是你得到No response descriptors match the response loaded.的原因。

因此,修复响应描述符路径模式。可能@"/api/ratepoint/:sid"就足够了。