0

私は RestKit をまったく使用したことがなく、POST リクエストを使用してシステムにログインしようとしています。私はRestKitバージョン0.20.3を使用していますが、これが私のやり方です:

- (IBAction)login:(id)sender {
    NSString *email = [self.emailTextField text];
    NSString *password = [self.passwordTextField text];

    NSURL *url = [[NSURL alloc] initWithString:@"http://myhost.com/api.php"];
    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:url];
    NSDictionary *tmp = @{@"rquest":@"user",
                          @"tag":@"login",
                          @"email":email,
                          @"password":password};
    [manager postObject:tmp path:@"" parameters:nil
                success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                    NSDictionary *result = [mappingResult dictionary];
                    if([[result objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithInt:1]]){
                        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
                        [def setBool:YES forKey:@"isLoggedIn"];
                        // set user details...
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                }
                failure:^(RKObjectRequestOperation *operation, NSError *error) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                    message:[error localizedDescription]
                                                                   delegate:nil
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles:nil];
                    [alert show];
                    NSLog(@"Hit error: %@", error);
                }];
}

ご覧のとおり、応答をオブジェクトにマップする必要はあまりないので、NSDictionary. これが問題かどうかはわかりませんが、上記のコードを実行しようとすると、次のエラーが表示されます。

013-10-06 11:24:51.897 Eateries[1182:454b] E restkit.network:RKResponseMapperOperation.m:304 Failed to parse response data: Loaded an unprocessable response (404) with content type 'application/json'
2013-10-06 11:24:51.902 Eateries[1182:1003] E restkit.network:RKObjectRequestOperation.m:243 POST 'http://myhost.com/api.php' (404 Not Found / 0 objects) [request=0.1479s mapping=0.0000s total=0.1648s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (404) with content type 'application/json'" UserInfo=0x8ee81e0 {NSErrorFailingURLKey=http://myhost.com/api.php, NSUnderlyingError=0x8ef4ea0 "The operation couldn’t be completed. (Cocoa error 3840.)", NSLocalizedDescription=Loaded an unprocessable response (404) with content type 'application/json'}
2013-10-06 11:24:51.978 Eateries[1182:a0b] Hit error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (404) with content type 'application/json'" UserInfo=0x8ee81e0 {NSErrorFailingURLKey=http://myhost.com/api.php, NSUnderlyingError=0x8ef4ea0 "The operation couldn’t be completed. (Cocoa error 3840.)", NSLocalizedDescription=Loaded an unprocessable response (404) with content type 'application/json'}

ここで何が間違っていたのかよくわからないので、本当に混乱しています。何か提案があれば、よろしくお願いします。ありがとうございました。

追伸: 個人的な目的でホストの名前を変更しましたが、他のプラットフォームからテストしようとすると、サーバーは実際に要求に対して正常に応答します。

4

1 に答える 1