1

我正在使用 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;
}

播放器.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'
4

2 回答 2

2

看起来您的服务器正在期待一个带有“玩家”的对象

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

你正在传递“/players.json”

参数:{"/players.json"=>{"age"=>"40", "name"=>"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];
于 2013-11-01T14:54:02.587 回答
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];
于 2013-11-01T12:22:31.853 回答