无法使用 RestKit POST 到服务器

Posted

技术标签:

【中文标题】无法使用 RestKit POST 到服务器【英文标题】:Unable to POST to Server using RestKit 【发布时间】:2013-10-23 12:33:23 【问题描述】:

我已经尝试了很长时间了,但是我无法成功地发布到我的服务器。我读过this、github object mapping overview for the Restkit 和Gist restKit tutorial。

我的问题

我正在尝试发布登录信息(昵称、硬件 ID、手机型号)并从我的服务器获取 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

感谢您的帮助。

【问题讨论】:

您能否显示您希望从服务器收到的响应 POST 的 JSON。 【参考方案1】:

您的问题似乎是:

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

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

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

【讨论】:

再次感谢 Wain 的精彩回复。我之前确实尝试过用响应映射切换请求映射的位置,但它之前没有用。我可能一起尝试了其他东西。

以上是关于无法使用 RestKit POST 到服务器的主要内容,如果未能解决你的问题,请参考以下文章

无法让 RestKit 0.2 发布参数

Restkit:从核心数据获取到服务器的 POST 对象数组

无法通过 RestKIT 0.22 上传二进制数据 - 服务器接收长度为 0 的流

Swift RestKit Post 不包括帖子数据

使用 Restkit 在 Core Data 中删除后保存对象使其崩溃

我在哪里可以找到 RestKit Post 源代码或教程?