1

我正在尝试为我的应用程序编写一个身份验证模块。我想检查用户之前是否保存过。为此,我必须检查我的数据库之前是否保存过该用户。

我打算向我的服务器后端发出 HTTP 请求。因此,我向服务器发出了一个示例服务器端请求,如下所示:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if( [identifier isEqualToString:@"login"]){

        NSString *curUrl = [self.serverUrl stringByAppendingString:@"/test"];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *parameters = @{@"foo": @"bar"};
        [manager POST:curUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"JSON: %@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];

        return YES;
    }else if ([identifier isEqualToString:@"signup"]) {
        return YES;
    }else{
        return NO;
    }
}

但是,我面临一个例外,如下所示:

*** Assertion failure in -[AFHTTPRequestOperation initWithRequest:],
 /myproj/Pods/AFNetworking/AFNetworking/AFURLConnectionOperation.m:164
2013-11-11 09:17:17.005 myproj[354:60b] *** Terminating app due to uncaught exception
 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: urlRequest'
*** First throw call stack:

(0x2eb05f53 0x38edc6af 0x2eb05e2d 0x2f4ad183 0x1a3d15 0x19cb17 0x19f3ad 0x19fb31 0xb7565 
0x3127b603 0x3127b3c1 0x31325637 0x31361d7b 0x313606b3 0x3135f705 0x3153a65b 0x312abf3f 
0x312abedf 0x312abeb9 0x31297b3f 0x312ab92f 0x312ab601 0x312a668d 0x3127ba25 0x3127a221 
0x2ead118b 0x2ead065b 0x2eacee4f 0x2ea39ce7 0x2ea39acb 0x3375a283 0x312dba41 0xb87a5 
0x393e4ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

为什么我会收到此错误?AFNetworking 的 HTTP Post 请求是异步的吗?

4

1 回答 1

0

为了解决“NSInternalInconsistencyException”问题,我们可以查看 AFHTTPRequestOperationManager 实现: AFHTTPRequestOperationManager POST:parameters:success:failure: method source code

它正在使用 self.baseURL 构造结果 URLString。您已经构建了它,因此结果 url 不正确。要修复此异常,请尝试更改您的 url 构造代码:

NSString *curUrl = @"/test";

或者:

NSString *curUrl = @"test";
于 2013-11-11T10:59:43.513 回答