【问题标题】:Error when posting using RestKit使用 RestKit 发布时出错
【发布时间】:2012-06-27 21:23:51
【问题描述】:

在成功将 RestKit 用于对象映射后,我一直在尝试使用它进行发布,并不断收到错误消息“找不到 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] 我 restkit:RKLog.m:33 RestKit 已初始化...

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

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

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

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

2012-06-27 17:58:41.262 MyTestApp[6101:fb03] Erro encontrado:错误域=org.restkit.RestKit.ErrorDomain Code=1001“找不到 keyPath 的对象映射:''”UserInfo= 0x808fac0 {=RKObjectMapperKeyPath, NSLocalizedDescription=找不到 keyPath 的对象映射:''}

【问题讨论】:

    标签: post restkit


    【解决方案1】:

    我认为这个代码块代表了主要问题:

    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 类中的标识符属性似乎被命名为 pid,如 mapAttributes: 调用中所示。在设置路由器的资源路径时,我认为 RestKit 不知道用什么替换 :id 的值,使用 KVC。

    【讨论】:

    • 非常感谢您的 helo,但我已经将 :id 更改为 :pid 并映射了其余属性,但仍然出现错误:1) "RKObjectMapper.m:81 添加映射错误:找不到keyPath的对象映射:''” 2)“RKObjectLoader.m:222在映射过程中遇到错误:找不到keyPath的对象映射:''”3)“RKObjectLoader.m:345遇到错误同时尝试从有效负载映射服务器端错误:找不到 keyPath 的对象映射:''”我现在尝试在 RKObjectLoader.m 中进行调试,看看是否发现了问题。此映射适用于 GET
    • 我想我在转换日期类型时遇到问题:value = "处理请求流时出错。将属性“DataNascimento”的请求负载中的值转换为类型“DateTime”时遇到错误,这是属性的预期类型。有关详细信息,请参阅内部异常。";
    • 我已经修复了,问题是我试图以字符串形式发布的数据库中的日期类型。当然,当我执行 GET 时,我对它没有任何问题,但是在发布它时会导致映射类型问题。感谢您的帮助塞巴斯蒂安。
    • 我很高兴你知道了。干杯!
    猜你喜欢
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多