使用AFNetworking Library在“shouldPerformSegueWithIdentifier”操作中执行HTTP Post请求

时间:2013-11-11 08:01:18

标签: ios afnetworking-2

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

我打算向我的服务器后端发出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

为什么会出现此错误? HTTP Post的AF Post请求是否是异步的?

1 个答案:

答案 0 :(得分:0)

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

使用self.baseURL构造结果URLString。你已经构造了这个,所以结果url是不正确的。要修复此异常,请尝试更改您的网址构造代码:

NSString *curUrl = @"/test";

或:

NSString *curUrl = @"test";