2013-10-06 43 views
0

我完全是RestKit的新手,我試圖使用POST請求登錄到我的系統。我使用RestKit版本0.20.3,這是我如何做:使用POST與RestKit登錄返回404錯誤

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

我真的很困惑,因爲我真的不知道我做錯了什麼在這裏。如果您有任何建議,請讓我知道。謝謝。 P:我只是爲了個人目的更改了主機的名稱,但是當我嘗試從其他平臺上測試時,我的服務器確實對請求做出了響應。

回答

0

您正在使用錯誤的API。

postObject:...用於發佈要映射的對象,這意味着object參數(如果不是nil)將用作映射響應的目標。

如果您不想映射響應,只需使用底層AFHTTPClient執行普通的POST請求。

[[RKObjectManager sharedManager].HTTPClient postPath:@"" parameters:tmp success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    // ... 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    // ... 
} 
+0

謝謝,這正是問題所在。 –