Post 使用RestKit过帐时出错

Post 使用RestKit过帐时出错,post,restkit,Post,Restkit,在我成功地将RestKit用于对象映射之后,我一直在尝试使用RestKit进行post,并不断收到错误“找不到keyPath的对象映射:“” 我已经成功地使用了iOS的JSON,代码如下: responseData = [NSMutableData data]; NSURL *url = [NSURL URLWithString:@"http://lalubema.cloudapp.net:9000/Data.soap/Pessoa"]; NSMutableDictionary *param

在我成功地将RestKit用于对象映射之后,我一直在尝试使用RestKit进行post,并不断收到错误“找不到keyPath的对象映射:“”

我已经成功地使用了iOS的JSON,代码如下:

responseData = [NSMutableData data];

NSURL *url = [NSURL URLWithString:@"http://lalubema.cloudapp.net:9000/Data.soap/Pessoa"];

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"Objective-c test from send Request #1" forKey:@"Nome"];
[params setObject:@"/Date(1200355200000)/" forKey:@"DataNascimento"];   
[params setObject:@"M" forKey:@"Sexo"];
[params setObject:@"(31)2321-2383" forKey:@"Telefone"];

NSError *jsonError;
NSData *requestdata = [NSJSONSerialization dataWithJSONObject:params options:0 error:&jsonError];

NSMutableURLRequest *request;
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestdata length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestdata];

//this kicks off the request asynchronously
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
我正在尝试使用RestKit并得到错误的代码如下:

[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;
[RKObjectManager sharedManager].acceptMIMEType = RKMIMETypeJSON;

RKObjectMapping* pessoaMapping = [RKObjectMapping mappingForClass:[Pessoa class]];
pessoaMapping.setDefaultValueForMissingAttributes = YES;
[pessoaMapping mapKeyPath:@"DataNascimento" toAttribute:@"dataNascimento"];
[pessoaMapping mapKeyPath:@"Nome" toAttribute:@"nome"];
[pessoaMapping mapKeyPath:@"Sexo" toAttribute:@"sexo"];
[pessoaMapping mapKeyPath:@"Telefone" toAttribute:@"telefone"];
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:pessoaMapping]; 

RKObjectMapping* pessoaSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[pessoaSerializationMapping mapAttributes:@"dataNascimento", @"docIdentidade", @"email", @"foto", @"pid", @"nome", @"senha", @"sexo", @"telefone", nil];    
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:pessoaSerializationMapping forClass:[Pessoa class]];

RKObjectRouter *router = [[RKObjectManager sharedManager] router];

[router routeClass:[Pessoa class] toResourcePath:@"/Pessoa/:id"];
[router routeClass:[Pessoa class] toResourcePath:@"/Pessoa" forMethod:RKRequestMethodPOST];

Pessoa *Novo = [[Pessoa alloc] init];

Novo.nome = @"Objective-c test from send postPessoa #1";
Novo.telefone = @"(31)1234-5678";
Novo.sexo = @"M";

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MM-yyyy"];    

RKDotNetDateFormatter *dotNetFormattedDate = [[RKDotNetDateFormatter alloc] init];
NSString *data = [dotNetFormattedDate stringFromDate:[df dateFromString: @"10-10-1979"]];

Novo.dataNascimento = data;

// POST to /Pessoa
[[RKObjectManager sharedManager] postObject:Novo delegate:self];
RKLog错误为:

2012-06-27 17:58:28.539 MyTestApp[6101:fb03]I restkit:RKLog.m:33 restkit已初始化

2012-06-27 17:58:28.589 MyTestApp[6101:fb03]I restkit.网络.可达性:RKReachabilityObserver.m:369已为可达性观察员确定网络可用性

2012-06-27 17:58:41.260 MyTestApp[6101:fb03]W restkit对象映射:RKObjectMapper.m:81添加映射错误:找不到keyPath的对象映射:“”

2012-06-27 17:58:41.261 MyTestApp[6101:fb03]E restkit.网络:RKObjectLoader.m:222在映射过程中遇到错误:找不到keyPath的对象映射:“”

2012-06-27 17:58:41.261 MyTestApp[6101:fb03]E restkit.网络:RKObjectLoader.m:345在尝试从负载映射服务器端错误时遇到错误:找不到keyPath的对象映射:“”


2012-06-27 17:58:41.262 MyTestApp[6101:fb03]Erro encontrado:Error Domain=org.restkit.restkit.ErrorDomain Code=1001“找不到键路径的对象映射:“”“UserInfo=0x808fac0{=RKObjectMapperKeyPath,NSLocalizedDescription=找不到键路径的对象映射:“””

我认为主要问题由以下代码块表示:

RKObjectMapping* pessoaMapping = [RKObjectMapping mappingForClass:[Pessoa class]];
pessoaMapping.setDefaultValueForMissingAttributes = YES;
[pessoaMapping mapKeyPath:@"DataNascimento" toAttribute:@"dataNascimento"];
[pessoaMapping mapKeyPath:@"Nome" toAttribute:@"nome"];
[pessoaMapping mapKeyPath:@"Sexo" toAttribute:@"sexo"];
[pessoaMapping mapKeyPath:@"Telefone" toAttribute:@"telefone"];
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:pessoaMapping]; 

RKObjectMapping* pessoaSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[pessoaSerializationMapping mapAttributes:@"dataNascimento", @"docIdentidade", @"email", @"foto", @"pid", @"nome", @"senha", @"sexo", @"telefone", nil];    
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:pessoaSerializationMapping forClass:[Pessoa class]];
您应该尝试替换为:

RKObjectMapping* pessoaMapping = [RKObjectMapping mappingForClass:[Pessoa class]];
pessoaMapping.setDefaultValueForMissingAttributes = YES;
[pessoaMapping mapKeyPath:@"DataNascimento" toAttribute:@"dataNascimento"];
[pessoaMapping mapKeyPath:@"Nome" toAttribute:@"nome"];
[pessoaMapping mapKeyPath:@"Sexo" toAttribute:@"sexo"];
[pessoaMapping mapKeyPath:@"Telefone" toAttribute:@"telefone"];
// up until this point, the code is the same, changes below
// -------------------------------------------------------------------------------
// map rest of the attributes, considering key paths match attribute names exactly
[pessoaMapping mapAttributes:@"docIdentidade", @"email", @"foto", @"pid", @"senha", nil];
// set the serialization mapping
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[pessoaMapping inverseMapping] forClass:[Pessoa class]];
此外,另一个问题出现在以下行:

[router routeClass:[Pessoa class] toResourcePath:@"/Pessoa/:id"];

Pessoa类中的identifier属性似乎命名为pid,如mapAttributes:call中所示。设置路由器的资源路径时,我认为RestKit不知道使用KVC用什么替换:id的值。

非常感谢您的帮助,但我已经将:id更改为:pid并映射了其余属性,但我仍然得到错误:1)RKObjectMapper.m:81添加映射错误:找不到keyPath:“2”的对象映射RKObjectLoader.m:222在映射过程中遇到错误:找不到keyPath:“3”的对象映射RKObjectLoader.m:345在尝试从有效负载映射服务器端错误时遇到错误:找不到keyPath的对象映射:“”“我正在尝试在RKObjectLoader.m内部进行调试,以查看是否找到错误。此映射适用于Get。我想我在转换日期类型时遇到问题:value=“error processing request stream”。将属性“DataNascimento”的请求负载值转换为属性的预期类型“DateTime”类型时遇到错误。请参阅内部异常以了解更多详细信息。“我已经修复了它,问题是我试图以字符串形式发布的数据库中的日期类型。当我执行GET时,当然没有问题,但发布时会导致映射类型问题。感谢您的帮助Sebastian。我很高兴您能够解决。干杯!