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'}

我真的很困惑,因为我真的不知道我在这里做错了什么。如果您有任何建议,请告诉我。谢谢你。

Ps:我只是出于个人目的更改了我的主机名称,但是当我尝试从其他平台测试它时,我的服务器确实响应了该请求。

4

1 回答 1

0

您使用了错误的 API。

postObject:...用于发布要映射的对象,这意味着该object参数(如果不是nil)将用作映射响应的目标。

如果您不想映射响应,只需使用底层AFHTTPClient执行简单的 POST 请求。

[[RKObjectManager sharedManager].HTTPClient postPath:@"" parameters:tmp success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // ...
}
于 2013-10-06T04:46:03.003 回答