POST到服务器

时间:2015-12-10 13:59:55

标签: ios objective-c iphone json restkit

我正在接受以下类型的请求发送服务

 {"ClientDetails":{"ChauffeurId":0,"FirstName":"String","LastName":"String","Email":"String","Password":"String","PhoneNumber":"String","Thumbnail":"AA==","PayKey":"String","DeviceKey":"String"},"Token":"String"}

我使用以下代码使用RestKit映射发送此请求,如图所示,问题是我收到'JSON write(Client)中的无效类型'错误,无法弄清楚错误是什么。

    RKObjectMapping *requestMapping = [RKObjectMapping mappingForClass:[Client class]];
    [requestMapping addAttributeMappingsFromDictionary:@{ @"FirstName": @"FirstName", @"LastName": @"LastName",@"Email":@"Email",@"PhoneNumber":@"PhoneNumber",@"Token":@"PayKey",@"Password":@"Password",@"ChauffeurId":@"ChauffeurId",@"Image":@"Thumbnail",@"DeviceToken":@"DeviceKey" }];


    RKObjectMapping *tokenMapping = [RKObjectMapping requestMapping];
    [tokenMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"token" toKeyPath:@"Token"]];
    [tokenMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"client"
                                                                               toKeyPath:@"ClientDetails"
                                                                             withMapping:requestMapping]];

    RKRequestDescriptor *tokenReqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:tokenMapping
                                                                                   objectClass:[RegClientRequest class]
                                                                                   rootKeyPath:nil method:RKRequestMethodPOST];

   // Register our descriptors with a manager
    RKObjectManager *manager = [RKObjectManager sharedManager];

    [manager addRequestDescriptor:tokenReqDescriptor];

    Client *client = [Client new];
    NewUser *user = [NewUser sharedNewUser];
    client.FirstName = user.strFname;
    client.LastName = user.strLname;
    client.Password = user.strPwd;
    client.PhoneNumber = user.strNum;
    client.Email = user.strEmail;
    client.Token = user.strToken;
    client.DeviceToken = user.strDeviceToken;

    RegClientRequest *regClientRequest = [RegClientRequest new];
    regClientRequest.token = [ServiceUrls token];
    regClientRequest.client = client;

    [manager postObject:regClientRequest path:[ServiceUrls registerUser] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {   
     }

1 个答案:

答案 0 :(得分:1)

问题是您要创建特定于班级的映射,您应该使用[RKObjectMapping requestMapping或使用inverseMapping

这意味着,此时您正在将客户端映射到客户端并尝试将生成的客户端放入JSON(而不是将客户端映射到字典)。