RestKit如何发布对象

时间:2013-10-31 17:54:58

标签: ios objective-c restkit restkit-0.20

我正在使用Rest Kit 0.20.3和Xcode 5来创建应用程序。我已完成GET请求并从服务器获取了我的2个对象。现在我创建了一个带有两个文本字段的附加视图来输入playerName和playerAge。 我已经尝试使用以下代码将新播放器发布到服务器但它无法正常工作:

AddPlayerViewController.h

#import <RestKit/RestKit.h>
#import "Player.h"

@interface AddPlayerViewController : UIViewController

@property (nonatomic, strong) Player *addedPlayer;

@property (weak, nonatomic) IBOutlet UITextField *playerNameTxt;
@property (weak, nonatomic) IBOutlet UITextField *playerAgeTxt;

- (IBAction)save:(id)sender;
@end

AddPlayerViewController.m

@implementation AddPlayerViewController

- (IBAction)save:(id)sender {

    [self postObject];
}

-(void)postObject{

    [[RKObjectManager sharedManager] postObject:addedPlayer path:@"/players.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        addedPlayer.playerName = playerNameTxt.text;
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
        [numberFormatter setNumberStyle:NSNumberFormatterNoStyle];
        addedPlayer.playerAge = [numberFormatter numberFromString:[NSString stringWithFormat:@"%@",playerAgeTxt.text]];
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
    }];
}
@end

我得到以下错误:

Expected status code in (200), got 422, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xaf67e60> { URL: http://localhost:3000/players.json}

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    RKLogConfigureByName("RestKit", RKLogLevelWarning);
    RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);

    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://192.168.1.3:3010"]];

    RKObjectMapping *playerMapping = [RKObjectMapping mappingForClass:[Player class]];

    [playerMapping addAttributeMappingsFromDictionary:@{@"id": @"playerID",
                                                        @"name": @"playerName",
                                                        @"age" : @"playerAge",
                                                        @"created_at": @"createdAt",
                                                        @"updated_at": @"updatedAt"}];



    RKResponseDescriptor *responseDesc = [RKResponseDescriptor responseDescriptorWithMapping:playerMapping method:RKRequestMethodAny pathPattern:@"/players.json" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];

    [objectManager addResponseDescriptor:responseDesc];


    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[playerMapping inverseMapping] objectClass:[Player class] rootKeyPath:@"/players.json" method:RKRequestMethodPOST];

    [objectManager addRequestDescriptor:requestDescriptor];
    return YES;
}

Player.h

@interface Player : NSObject

@property (nonatomic,strong) NSNumber *playerID;
@property (nonatomic,strong) NSString *playerName;
@property (nonatomic,strong) NSNumber *playerAge;
@property (nonatomic,strong) NSDate *createdAt;
@property (nonatomic,strong) NSDate *updatedAt;

@end

在服务器端,我得到了这个:

Started POST "/players.json" for 192.168.1.3 at 2013-11-01 20:01:34 +0530
Processing by PlayersController#create as JSON
  Parameters: {"/players.json"=>{"age"=>"40", "name"=>"sachin"}}
Completed 400 Bad Request in 0ms

ActionController::ParameterMissing (param not found: player):
  app/controllers/players_controller.rb:73:in `player_params'
  app/controllers/players_controller.rb:28:in `create'

2 个答案:

答案 0 :(得分:2)

看起来你的服务器期待一个带有“播放器”的对象

ActionController :: ParameterMissing(未找到参数:播放器):

你正在传递“/players.json”

参数:{“/ players.json”=&gt; {“”“&gt;”40“,”name“=&gt;”sachin“}}

尝试将您的帖子路径更改为Xcode中的“播放器”

[[RKObjectManager sharedManager] postObject:addedPlayer path:@"player" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[playerMapping inverseMapping] objectClass:[Player class] rootKeyPath:@"player" method:RKRequestMethodPOST];

答案 1 :(得分:1)

我建议您这样做:

addedPlayer.playerName = playerNameTxt.text;

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setNumberStyle:NSNumberFormatterNoStyle];
addedPlayer.playerAge = [numberFormatter numberFromString:[NSString stringWithFormat:@"%@",playerAgeTxt.text]];

[[RKObjectManager sharedManager] postObject:addedPlayer path:@"/players.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"Yay - life is great!");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Bum: %@", error);
}];

从您更新的日志:

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[playerMapping inverseMapping] objectClass:[Player class] rootKeyPath:@"player" method:RKRequestMethodPOST];