RestKit didFailWithError 总是被调用,didLoadObjects 从未被调用

发布于 2024-12-21 01:53:37 字数 9592 浏览 0 评论 0原文

嗯,标题几乎说明了一切。我正在调用我的服务器,当它成功或失败时,它会调用我的 didFailWithError 方法,而不会调用我的 didLoadObjects 方法。我本来希望在收到 401 返回值时调用 didFailWithError ,而在收到 200 返回值时调用 didLoadObjects 。

以下是我如何设置对象映射:

- (void)setupCreateAccountMapping
{
    // mapping for CreateAccount
    RKObjectMapping* createAccountSerializationMapping = [RKObjectMapping mappingForClass:[CreateAccount class]];
    [createAccountSerializationMapping mapAttributes:@"email", @"pwd", @"uname", nil];
    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:createAccountSerializationMapping forClass:[CreateAccount class]];

    // mapping for CheckUserName
    RKObjectMapping* checkUserNameSerializationMapping = [RKObjectMapping mappingForClass:[CheckUserName class]];
    [checkUserNameSerializationMapping mapAttributes:@"uname", nil];
    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:checkUserNameSerializationMapping forClass:[CheckUserName class]];    

    router = [RKObjectRouter new] ;
    [router routeClass:[CheckUserName class] toResourcePath:@"/registration/rest/users/isavailable" forMethod:RKRequestMethodGET];    
    [router routeClass:[CreateAccount class] toResourcePath:@"/registration/rest/users/create_account" forMethod:RKRequestMethodPOST];

    [RKObjectManager sharedManager].router = router; 
}

以下是我如何发布到服务器。

- (IBAction)submitPressed:(id)sender {

    CreateAccount* user = [CreateAccount new];
    user.email = [emailAddressTextField text];
    user.uname = [usernameTextField text];
    user.pwd = [passwordTextField text];

    [[RKObjectManager sharedManager] postObject:user delegate:self];
}

在 didFailWithError 中,我检查:

if ([response statusCode] >= 400) {

看看我是否成功,这看起来真的非常错误。

我在这里做错了什么?当我运行示例代码时,我确实看到 didLoadObjects 被调用。

这是两次运行的日志,第一次失败,第二次成功:

在这里我们可以看到我试图创建一个名为“andrew”的用户,但该用户已经存在。我收到 406 返回并调用 didFailWithError 。

2011-12-14 10:52:33.376 Ferret[60007:f803] D restkit.network:RKClient.m:389 Reachability to host '127.0.0.1' determined for client <RKClient: 0x900c9d0>, unsuspending queue <RKRequestQueue: 0x900eee0 name=(null) suspended=YES requestCount=0 loadingCount=0/5>
Current language:  auto; currently objective-c
2011-12-14 10:53:16.478 Ferret[60007:f803] D restkit.network:RKRequest.m:362 Sending asynchronous POST request to URL http://127.0.0.1:8080/registration/rest/users/create_account.
2011-12-14 10:53:16.478 Ferret[60007:f803] D restkit.network:RKObjectLoader.m:302 POST or PUT request for source object <CreateAccount: 0x6d1d8e0>, serializing to MIME Type application/x-www-form-urlencoded for transport...
2011-12-14 10:53:16.479 Ferret[60007:f803] D restkit.object_mapping:RKObjectMappingOperation.m:509 Starting mapping operation...
2011-12-14 10:53:16.479 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:510 Performing mapping operation: RKObjectMappingOperation for '__NSCFDictionary' object. Mapping values from object <CreateAccount: 0x6d1d8e0> to object {
} with object mapping RKObjectMapping class => CreateAccount: keyPath mappings => (
    "RKObjectKeyPathMapping: pwd => pwd",
    "RKObjectKeyPathMapping: uname => uname",
    "RKObjectKeyPathMapping: email => email"
)
2011-12-14 10:53:16.479 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'pwd' to 'pwd'
2011-12-14 10:53:16.479 Ferret[60007:f803] D restkit.object_mapping:RKObjectPropertyInspector.m:107 Cached property names and types for Class 'CreateAccount': {
    accessibilityHint = NSString;
    accessibilityLabel = NSString;
    accessibilityLanguage = NSString;
    accessibilityValue = NSString;
    email = NSString;
    pwd = NSString;
    uname = NSString;
}
2011-12-14 10:53:16.482 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'pwd' to 'pwd'. Value: 123456
2011-12-14 10:53:16.483 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'uname' to 'uname'
2011-12-14 10:53:16.483 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'uname' to 'uname'. Value: andrew
2011-12-14 10:53:16.483 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'email' to 'email'
2011-12-14 10:53:16.483 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'email' to 'email'. Value: [email protected]
2011-12-14 10:53:16.483 Ferret[60007:f803] D restkit.object_mapping:RKObjectMappingOperation.m:516 Finished mapping operation successfully...
2011-12-14 10:53:16.484 Ferret[60007:f803] T restkit.network:RKRequest.m:310 Prepared POST URLRequest '<NSMutableURLRequest http://127.0.0.1:8080/registration/rest/users/create_account>'. HTTP Headers: {
    Accept = "application/json";
    "Content-Type" = "application/x-www-form-urlencoded";
}. HTTP Body: pwd=123456&uname=andrew&email=foo%40example.com.
2011-12-14 10:53:16.572 Ferret[60007:f803] D restkit.network:RKResponse.m:196 NSHTTPURLResponse Status Code: 406
2011-12-14 10:53:16.573 Ferret[60007:f803] D restkit.network:RKResponse.m:197 Headers: {
    "Content-Length" = 32;
    "Content-Type" = "application/json";
    Date = "Wed, 14 Dec 2011 15:53:14 GMT";
    Server = "Apache-Coyote/1.1";
}
2011-12-14 10:53:16.573 Ferret[60007:f803] T restkit.network:RKResponse.m:202 Read response body: UserName or Email must be unique
2011-12-14 10:53:16.573 Ferret[60007:f803] E restkit.network:RKObjectLoader.m:289 Encountered an error while attempting to map server side errors from payload: Unexpected token, wanted '{', '}', '[', ']', ',', ':', 'true', 'false', 'null', '"STRING"', 'NUMBER'.

在这里我们可以看到我尝试创建一个名为“andrewA”的用户并且该用户已创建。我收到 22 返回,并调用 didFailWithError,而不是 didLoadObjects

2011-12-14 10:54:21.486 Ferret[60007:f803] D restkit.network:RKRequest.m:362 Sending asynchronous POST request to URL http://127.0.0.1:8080/registration/rest/users/create_account.
2011-12-14 10:54:21.486 Ferret[60007:f803] D restkit.network:RKObjectLoader.m:302 POST or PUT request for source object <CreateAccount: 0x9041300>, serializing to MIME Type application/x-www-form-urlencoded for transport...
2011-12-14 10:54:21.486 Ferret[60007:f803] D restkit.object_mapping:RKObjectMappingOperation.m:509 Starting mapping operation...
2011-12-14 10:54:21.486 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:510 Performing mapping operation: RKObjectMappingOperation for '__NSCFDictionary' object. Mapping values from object <CreateAccount: 0x9041300> to object {
} with object mapping RKObjectMapping class => CreateAccount: keyPath mappings => (
    "RKObjectKeyPathMapping: pwd => pwd",
    "RKObjectKeyPathMapping: uname => uname",
    "RKObjectKeyPathMapping: email => email"
)
2011-12-14 10:54:21.486 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'pwd' to 'pwd'
2011-12-14 10:54:21.486 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'pwd' to 'pwd'. Value: 123456
2011-12-14 10:54:21.494 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'uname' to 'uname'
2011-12-14 10:54:21.494 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'uname' to 'uname'. Value: andrewA
2011-12-14 10:54:21.494 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'email' to 'email'
2011-12-14 10:54:21.494 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'email' to 'email'. Value: [email protected]
2011-12-14 10:54:21.494 Ferret[60007:f803] D restkit.object_mapping:RKObjectMappingOperation.m:516 Finished mapping operation successfully...
2011-12-14 10:54:21.495 Ferret[60007:f803] T restkit.network:RKRequest.m:310 Prepared POST URLRequest '<NSMutableURLRequest http://127.0.0.1:8080/registration/rest/users/create_account>'. HTTP Headers: {
    Accept = "application/json";
    "Content-Type" = "application/x-www-form-urlencoded";
}. HTTP Body: pwd=123456&uname=andrewA&email=fooA%40example.com.
2011-12-14 10:54:23.004 Ferret[60007:f803] D restkit.network:RKResponse.m:196 NSHTTPURLResponse Status Code: 200
2011-12-14 10:54:23.004 Ferret[60007:f803] D restkit.network:RKResponse.m:197 Headers: {
    "Content-Length" = 45;
    "Content-Type" = "application/json";
    Date = "Wed, 14 Dec 2011 15:54:20 GMT";
    Server = "Apache-Coyote/1.1";
    "X-Powered-By" = "Servlet/3.0; JBossAS-6";
}
2011-12-14 10:54:23.004 Ferret[60007:f803] T restkit.network:RKResponse.m:202 Read response body: 98a454470b8b1cf0d3e081f3e2ac2ca41323878059558
2011-12-14 10:54:23.016 Ferret[60007:1531b] D restkit.network:RKObjectLoader.m:214 No object mapping provider, using mapping provider from parent object manager to perform KVC mapping

请注意,在这两种情况下,我都从服务器返回非 JSON 响应,但它只是一个字符串,如错误消息或激活代码。

Well, the title pretty much says it all. I'm calling my server and when it suceeds or fails it calls my didFailWithError method, never calling my didLoadObjects method. I would have expected didFailWithError to be called when, say, I get a 401 back, and didLoadObjects to be called when I get a 200 back.

Here's how I set up my object mapping:

- (void)setupCreateAccountMapping
{
    // mapping for CreateAccount
    RKObjectMapping* createAccountSerializationMapping = [RKObjectMapping mappingForClass:[CreateAccount class]];
    [createAccountSerializationMapping mapAttributes:@"email", @"pwd", @"uname", nil];
    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:createAccountSerializationMapping forClass:[CreateAccount class]];

    // mapping for CheckUserName
    RKObjectMapping* checkUserNameSerializationMapping = [RKObjectMapping mappingForClass:[CheckUserName class]];
    [checkUserNameSerializationMapping mapAttributes:@"uname", nil];
    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:checkUserNameSerializationMapping forClass:[CheckUserName class]];    

    router = [RKObjectRouter new] ;
    [router routeClass:[CheckUserName class] toResourcePath:@"/registration/rest/users/isavailable" forMethod:RKRequestMethodGET];    
    [router routeClass:[CreateAccount class] toResourcePath:@"/registration/rest/users/create_account" forMethod:RKRequestMethodPOST];

    [RKObjectManager sharedManager].router = router; 
}

and here is how I post to the server.

- (IBAction)submitPressed:(id)sender {

    CreateAccount* user = [CreateAccount new];
    user.email = [emailAddressTextField text];
    user.uname = [usernameTextField text];
    user.pwd = [passwordTextField text];

    [[RKObjectManager sharedManager] postObject:user delegate:self];
}

In didFailWithError I check:

if ([response statusCode] >= 400) {

to see if I succeeded or not and that seems really, really, wrong.

What am I doing wrong here? When I run the sample code I do see didLoadObjects called.

Here is the log from 2 runs, the first unsuccessful and the second successful:

Here we can see me trying to create a user named "andrew" but one already exists. I get a 406 back and didFailWithError is called.

2011-12-14 10:52:33.376 Ferret[60007:f803] D restkit.network:RKClient.m:389 Reachability to host '127.0.0.1' determined for client <RKClient: 0x900c9d0>, unsuspending queue <RKRequestQueue: 0x900eee0 name=(null) suspended=YES requestCount=0 loadingCount=0/5>
Current language:  auto; currently objective-c
2011-12-14 10:53:16.478 Ferret[60007:f803] D restkit.network:RKRequest.m:362 Sending asynchronous POST request to URL http://127.0.0.1:8080/registration/rest/users/create_account.
2011-12-14 10:53:16.478 Ferret[60007:f803] D restkit.network:RKObjectLoader.m:302 POST or PUT request for source object <CreateAccount: 0x6d1d8e0>, serializing to MIME Type application/x-www-form-urlencoded for transport...
2011-12-14 10:53:16.479 Ferret[60007:f803] D restkit.object_mapping:RKObjectMappingOperation.m:509 Starting mapping operation...
2011-12-14 10:53:16.479 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:510 Performing mapping operation: RKObjectMappingOperation for '__NSCFDictionary' object. Mapping values from object <CreateAccount: 0x6d1d8e0> to object {
} with object mapping RKObjectMapping class => CreateAccount: keyPath mappings => (
    "RKObjectKeyPathMapping: pwd => pwd",
    "RKObjectKeyPathMapping: uname => uname",
    "RKObjectKeyPathMapping: email => email"
)
2011-12-14 10:53:16.479 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'pwd' to 'pwd'
2011-12-14 10:53:16.479 Ferret[60007:f803] D restkit.object_mapping:RKObjectPropertyInspector.m:107 Cached property names and types for Class 'CreateAccount': {
    accessibilityHint = NSString;
    accessibilityLabel = NSString;
    accessibilityLanguage = NSString;
    accessibilityValue = NSString;
    email = NSString;
    pwd = NSString;
    uname = NSString;
}
2011-12-14 10:53:16.482 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'pwd' to 'pwd'. Value: 123456
2011-12-14 10:53:16.483 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'uname' to 'uname'
2011-12-14 10:53:16.483 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'uname' to 'uname'. Value: andrew
2011-12-14 10:53:16.483 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'email' to 'email'
2011-12-14 10:53:16.483 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'email' to 'email'. Value: [email protected]
2011-12-14 10:53:16.483 Ferret[60007:f803] D restkit.object_mapping:RKObjectMappingOperation.m:516 Finished mapping operation successfully...
2011-12-14 10:53:16.484 Ferret[60007:f803] T restkit.network:RKRequest.m:310 Prepared POST URLRequest '<NSMutableURLRequest http://127.0.0.1:8080/registration/rest/users/create_account>'. HTTP Headers: {
    Accept = "application/json";
    "Content-Type" = "application/x-www-form-urlencoded";
}. HTTP Body: pwd=123456&uname=andrew&email=foo%40example.com.
2011-12-14 10:53:16.572 Ferret[60007:f803] D restkit.network:RKResponse.m:196 NSHTTPURLResponse Status Code: 406
2011-12-14 10:53:16.573 Ferret[60007:f803] D restkit.network:RKResponse.m:197 Headers: {
    "Content-Length" = 32;
    "Content-Type" = "application/json";
    Date = "Wed, 14 Dec 2011 15:53:14 GMT";
    Server = "Apache-Coyote/1.1";
}
2011-12-14 10:53:16.573 Ferret[60007:f803] T restkit.network:RKResponse.m:202 Read response body: UserName or Email must be unique
2011-12-14 10:53:16.573 Ferret[60007:f803] E restkit.network:RKObjectLoader.m:289 Encountered an error while attempting to map server side errors from payload: Unexpected token, wanted '{', '}', '[', ']', ',', ':', 'true', 'false', 'null', '"STRING"', 'NUMBER'.

Here we can see me trying to create a user named "andrewA" and the user is created. I get a 22 back and didFailWithError is called, not didLoadObjects

2011-12-14 10:54:21.486 Ferret[60007:f803] D restkit.network:RKRequest.m:362 Sending asynchronous POST request to URL http://127.0.0.1:8080/registration/rest/users/create_account.
2011-12-14 10:54:21.486 Ferret[60007:f803] D restkit.network:RKObjectLoader.m:302 POST or PUT request for source object <CreateAccount: 0x9041300>, serializing to MIME Type application/x-www-form-urlencoded for transport...
2011-12-14 10:54:21.486 Ferret[60007:f803] D restkit.object_mapping:RKObjectMappingOperation.m:509 Starting mapping operation...
2011-12-14 10:54:21.486 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:510 Performing mapping operation: RKObjectMappingOperation for '__NSCFDictionary' object. Mapping values from object <CreateAccount: 0x9041300> to object {
} with object mapping RKObjectMapping class => CreateAccount: keyPath mappings => (
    "RKObjectKeyPathMapping: pwd => pwd",
    "RKObjectKeyPathMapping: uname => uname",
    "RKObjectKeyPathMapping: email => email"
)
2011-12-14 10:54:21.486 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'pwd' to 'pwd'
2011-12-14 10:54:21.486 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'pwd' to 'pwd'. Value: 123456
2011-12-14 10:54:21.494 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'uname' to 'uname'
2011-12-14 10:54:21.494 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'uname' to 'uname'. Value: andrewA
2011-12-14 10:54:21.494 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:263 Mapping attribute value keyPath 'email' to 'email'
2011-12-14 10:54:21.494 Ferret[60007:f803] T restkit.object_mapping:RKObjectMappingOperation.m:273 Mapped attribute value from keyPath 'email' to 'email'. Value: [email protected]
2011-12-14 10:54:21.494 Ferret[60007:f803] D restkit.object_mapping:RKObjectMappingOperation.m:516 Finished mapping operation successfully...
2011-12-14 10:54:21.495 Ferret[60007:f803] T restkit.network:RKRequest.m:310 Prepared POST URLRequest '<NSMutableURLRequest http://127.0.0.1:8080/registration/rest/users/create_account>'. HTTP Headers: {
    Accept = "application/json";
    "Content-Type" = "application/x-www-form-urlencoded";
}. HTTP Body: pwd=123456&uname=andrewA&email=fooA%40example.com.
2011-12-14 10:54:23.004 Ferret[60007:f803] D restkit.network:RKResponse.m:196 NSHTTPURLResponse Status Code: 200
2011-12-14 10:54:23.004 Ferret[60007:f803] D restkit.network:RKResponse.m:197 Headers: {
    "Content-Length" = 45;
    "Content-Type" = "application/json";
    Date = "Wed, 14 Dec 2011 15:54:20 GMT";
    Server = "Apache-Coyote/1.1";
    "X-Powered-By" = "Servlet/3.0; JBossAS-6";
}
2011-12-14 10:54:23.004 Ferret[60007:f803] T restkit.network:RKResponse.m:202 Read response body: 98a454470b8b1cf0d3e081f3e2ac2ca41323878059558
2011-12-14 10:54:23.016 Ferret[60007:1531b] D restkit.network:RKObjectLoader.m:214 No object mapping provider, using mapping provider from parent object manager to perform KVC mapping

Notice in both cases I'm getting a non-JSON response back from the server, but it is just a string, like the error message or the activation code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2024-12-28 01:53:37

您将 Content-type 标头设置为“application/json”,并且响应不是格式良好的 JSON。我相信映射过程会由于解析问题而失败。您可以尝试修改服务器输出,例如返回 JSON 或设置适当的内容类型标头。

You have your Content-type header set to "application/json" and the response is not a well-formed JSON. I believe the mapping process bails out due to the parsing problem. You can try modifying the server output eg.return a JSON or to set appropriate content type header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文