RestKit didFailWithError всегда вызывается, didLoadObjects никогда не вызывается - PullRequest
0 голосов
/ 14 декабря 2011

Ну, название в значительной степени говорит само за себя.Я вызываю мой сервер, и когда он завершается успешно или терпит неудачу, он вызывает мой метод didFailWithError, никогда не вызывая мой метод didLoadObjects.Я ожидал, что didFailWithError будет вызываться, когда, скажем, я получу обратно 401, и didLoadObjects будет вызываться, когда я получу обратно 200.

Вот как я настроил сопоставление моего объекта:

- (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 вызывается.

Вот журнал из 2 запусков, первый неудачный и второй успешный:

Здесь мы видим, как я пытаюсь создать пользователяназывается "Андрей", но один уже существует.Я получаю обратно 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: foo@example.com
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: fooA@example.com
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 от сервера, но это просто строка, как сообщение об ошибкеили код активации.

1 Ответ

2 голосов
/ 14 декабря 2011

Ваш заголовок Content-type установлен на "application/json", и ответ не является правильно сформированным JSON. Я полагаю, что процесс отображения выручает из-за проблемы синтаксического анализа. Вы можете попробовать изменить выходные данные сервера, например, вернуть JSON или установить соответствующий заголовок типа контента.

...