9

我正在使用 AFNetworking 注册新用户,一切正常,但在以下块我有一些问题:

    AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:myRequest] autorelease];
operation.completionBlock = ^ {
    if ([operation hasAcceptableStatusCode]) {
        NSLog(@"success");
        username.backgroundColor = [UIColor yellowColor];
    } else {
        switch ([operation.response statusCode]) {
            case 421:                  
            {
                NSLog(@"Username taken.");
                username.backgroundColor = [UIColor yellowColor];   
            }
                break;
            default:
                break;
        }
    }
};

基本上,我的服务器端脚本会进行一些验证并返回一个 HTTP 状态代码(我知道 421 不是有效的)。这使我能够知道服务器上出了什么问题,这很好用。

我的问题是,当响应返回时,它会立即触发NSLog(@"success");orNSLog(@"Username taken.");但任何其他代码都会在几秒钟后触发。

任何人都可以对此有所了解吗?

4

2 回答 2

32

这是我的问题的解决方案,这要好得多,而且要快得多:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"success: %@", operation.responseString);

    [SVProgressHUD dismissWithSuccess:@"Sucess!" afterDelay:2];
    [self saveContinue:operation.responseString];


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

}
 ];

我希望这对人们有所帮助。

于 2011-11-30T19:31:20.223 回答
0

我的 HTTP POST 解决方案是这样的

NSData *data = [self.postBody dataUsingEncoding:NSUTF8StringEncoding];
            NSURL *url = [NSURL URLWithString:self.requestUrl];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:url];
            [request addValue:@"application/octet-stream" forHTTPHeaderField: @"Content-Type"];
            [request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forHTTPHeaderField:@"Content-Length"];
            [request setHTTPMethod:@"POST"];
            NSMutableData *requestBody = [NSMutableData data];
            [requestBody appendData:data];
            [request setHTTPBody:requestBody];
            AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
            operation.responseSerializer = [AFJSONResponseSerializer serializer];
            operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
            [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSInteger statusCode = operation.response.statusCode;
                [self requestFinished:responseObject andStatusCode:statusCode];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [self requestFailed:error];
            }];
            [[self.requestManager operationQueue] addOperation:operation];

            [AFHTTPRequestOperation batchOfRequestOperations:[NSArray arrayWithObjects:operation, nil] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
            } completionBlock:^(NSArray *operations) {
            }];

在这种情况下,它会将操作管理器上的单个操作排入队列。

于 2015-05-06T15:33:18.587 回答