2015-08-18 186 views
0

我遇到了無法連接到API的問題。這隻發生在iOS8上。它適用於iOS7。AFNetworking請求失敗問題

- (void)performSearch:(NSString*)type : (NSString*)keyword{ 
    url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", API, keyword]]; 
    request = [NSURLRequest requestWithURL:url]; 
    operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    credential = [NSURLCredential credentialWithUser:USERNAME password:PASSWORD persistence:NSURLCredentialPersistenceForSession]; 
    [operation setCredential:credential]; 

    [[NSOperationQueue mainQueue] addOperation:operation]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSError *error = nil; 
     NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error]; 

     if (error) { 
      NSLog(@"Error serializing %@", error); 
      return; 
     } 

     [self parseJSON:JSON :keyword]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"error : %@", error); 
    }]; 

    [operation start]; 
} 

我收到的錯誤消息。

Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo=0x7ff0650052f0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7ff062c5e4d0> { URL: https://distribution.virk.dk/cvr-permanent-prod-20150622/_search?q=anders } { status code: 401, headers { 
    Connection = "keep-alive"; 
    "Content-Length" = 194; 
    "Content-Type" = "text/html"; 
    Date = "Tue, 18 Aug 2015 09:36:09 GMT"; 
    Server = "nginx/1.6.2"; 
    "Www-Authenticate" = "Basic realm=\"Beskyttet adgang\""; 
} }, NSErrorFailingURLKey=https://distribution.virk.dk/cvr-permanent-prod-20150622/_search?q=anders, com.alamofire.serialization.response.error.data=<3c68746d 6c3e0d0a 3c686561 643e3c74 69746c65 3e343031 20417574 686f7269 7a617469 6f6e2052 65717569 7265643c 2f746974 6c653e3c 2f686561 643e0d0a 3c626f64 79206267 636f6c6f 723d2277 68697465 223e0d0a 3c63656e 7465723e 3c68313e 34303120 41757468 6f72697a 6174696f 6e205265 71756972 65643c2f 68313e3c 2f63656e 7465723e 0d0a3c68 723e3c63 656e7465 723e6e67 696e782f 312e362e 323c2f63 656e7465 723e0d0a 3c2f626f 64793e0d 0a3c2f68 746d6c3e 0d0a>, NSLocalizedDescription=Request failed: unauthorized (401)} 

回答

0

狀態代碼是401,這意味着您的帳戶或密碼不正確。我建議你應該註銷參數,並找出它們是否爲空或其他任何東西

+0

這是正確的,因爲它在iOS7中運行良好 – Yrol