无法使用RestKit POST到服务器

时间:2013-10-23 12:33:23

标签: objective-c restkit afnetworking restkit-0.20

我已经尝试了很长一段时间了,但我无法成功发布到我的服务器上。我已阅读thisgithub object mapping overview for the RestkitGist restKit tutorial

我的问题

我正在尝试发布登录信息(昵称,hardwareId,手机型号)并从我的服务器获取AccountId。但是我从控制台得到了这个回复:

GuessTheImage[6832:70b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AccountsClass 0x8c5cba0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key AccountInfo.'

我认为是问题

我相信我正在弄乱我在映射部分之前存储数据的方式。但我不确定到底在哪里。

我正在尝试发布的JSON应该是这样的

{
  "DeviceType": "sample string 1",
  "HardwareId": "sample string 2",
  "NickName": "sample string 3",
  "AccountId": 4
}

该计划概述 我在AccountClass中创建了映射变量。从loginviewcontroller,我在用户单击登录按钮后获取设备类型,hardwareId和昵称,并将这些变量分配给AccountClass中的变量,如果之后将其映射到POST。 AccountClass.h

#import <Foundation/Foundation.h>

@interface AccountsClass : NSObject

@property (nonatomic, strong,retain)NSString *DeviceType;
@property (nonatomic,strong)NSNumber *AccountId;
@property (nonatomic,strong) NSString *NickName;
@property (nonatomic,strong) NSNumber *HardwareId;

@end

LoginViewController.h

#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *usernameTextField;
@property (strong, nonatomic) IBOutlet UIButton *submitButton;
@property (nonatomic,readonly) NSUUID *identifierForVendor;
@property(nonatomic, readonly, retain) NSString *model;
@property (nonatomic,readonly,retain)NSString *StoreIdentifierForVendor;
@property (nonatomic,readonly,retain)NSString *StoreTheModel;
- (IBAction)submit:(id)sender;
@property (nonatomic,strong)NSString *nickname;
@end

LoginViewController.m

#import "LoginViewController.h"
#import <RestKit/RestKit.h>
#import "AccountsClass.h"
@interface LoginViewController ()

@end

@implementation LoginViewController
@synthesize usernameTextField;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissmissKeyboard)];
    [self.view addGestureRecognizer:tap];
    [usernameTextField setDelegate:self];


}
-(void)loadPostRequest
{
    _StoreIdentifierForVendor = [[[UIDevice currentDevice]identifierForVendor]UUIDString];
    _StoreTheModel = [UIDevice currentDevice].model;
    _nickname = usernameTextField.text;
     AccountsClass *AccountInfo = [[AccountsClass alloc] init];
    AccountInfo.NickName = _nickname;
    NSNumberFormatter *hardwareIdentification = [[NSNumberFormatter alloc]init];
    [hardwareIdentification setNumberStyle:NSNumberFormatterScientificStyle];
    NSNumber *hwreID =[hardwareIdentification numberFromString:_StoreIdentifierForVendor];
    AccountInfo.HardwareId = hwreID;
    AccountInfo.DeviceType =_StoreTheModel;
      RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[AccountsClass class]];
   [responseMapping addAttributeMappingsFromArray:@[@"NickName", @"HardwareId", @"DeviceType",@"AccountId"]];

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *AccountDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:statusCodes];


    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
    [requestMapping addAttributeMappingsFromArray:@[@"AccountInfo.NickName", @"AccountInfo.HardwareId", @"AccountInfo.DeviceType",@"AccountInfo.AccountId"]];
    // For any object of class Article, serialize into an NSMutableDictionary using the given mapping and nest
    // under the 'article' key path
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[AccountInfo class] rootKeyPath:nil method:RKRequestMethodAny];
    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://picquiz.azurewebsites.net"]];
                                [manager addRequestDescriptor:requestDescriptor];
                                [manager addResponseDescriptor:AccountDescriptor];
                                // POST to create
                                [manager postObject:AccountInfo path:@"/Accounts" parameters:nil success:nil failure:nil];

}
-(void)dissmissKeyboard
{
    [usernameTextField resignFirstResponder];
}

- (IBAction)submit:(id)sender {
    [self loadPostRequest];
}
@end

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您的问题似乎是:

[requestMapping addAttributeMappingsFromArray:@[@"AccountInfo.NickName", @"AccountInfo.HardwareId", @"AccountInfo.DeviceType",@"AccountInfo.AccountId"]];

因为您尝试POST的数据没有这样的嵌套数据。它看起来应该是:

[requestMapping addAttributeMappingsFromArray:@[@"NickName", @"HardwareId", @"DeviceType",@"AccountId"]];