8

我正在使用 AFNetworking 并创建一个我需要 json 反馈的发布请求。下面的代码有效,但是我有两个主要问题;我在哪里释放 ActivityIndi​​cator 管理器?第二个问题是这段代码是正确的,因为是新的,我对块感到困惑,所以我真的想知道我是否为了获得最佳性能而做正确的事情,即使它有效。

    NSURL *url = [NSURL URLWithString:@"mysite/user/signup"];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

    AFNetworkActivityIndicatorManager * newactivity = [[AFNetworkActivityIndicatorManager alloc] init]; 
    newactivity.enabled = YES;
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            usernamestring, @"login[username]",
                            emailstring, @"login[email]",
                            nil];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"mysite/user/signup"parameters:params];
    [httpClient release];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id json) {

        NSString *status = [json valueForKey:@"status"];  
        if ([status isEqualToString:@"success"]) {
            [username resignFirstResponder];
            [email resignFirstResponder];
            [self.navigationController dismissModalViewControllerAnimated:NO];
        }
        else {
            UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Login Unsuccessful"
                                                           message:@"Please try again"
                                                          delegate:NULL 
                                                 cancelButtonTitle:@"OK" 
                                                 otherButtonTitles:NULL];

            [alert show];
            [alert release];
        }

    }

    failure:^(NSHTTPURLResponse *response, NSError *error) {

    NSLog(@"%@", error);
    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Login Unsuccessful"
                                                       message:@"There was a problem connecting to the network!"
                                                      delegate:NULL 
                                             cancelButtonTitle:@"OK" 
                                             otherButtonTitles:NULL];

        [alert show];
        [alert release];


    }];

    NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
    [queue addOperation:operation];
    NSLog(@"check");    


}    

非常感谢您提前提供的帮助:)

4

2 回答 2

8

我知道这个问题有点老了,但我仍然想做出贡献。

正如 steveOhh 所说,您应该使用[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]来打开活动网络指示灯。它是一个单例,因此它不需要您手动分配初始化和释放。至于另一个问题,我注意到您在块调用中缺少一些参数,您也可以这样做,这是更简洁的代码:

NSURL *url = [NSURL URLWithString:@"mysite/user/signup"];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:[NSURLRequest requestWithURL:url] success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    // your success code here
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    // your failure code here
}];

[operation start]; // start your operation directly, unless you really need to use a queue
于 2012-01-19T03:11:55.933 回答
2

为什么不使用它呢?

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];

因此不需要分配和初始化

其他代码不能多说,刚开始学习objective-C和AFNetworking.. :)

问候,史蒂夫0hh

于 2011-10-09T16:40:20.583 回答