通过json反馈AFNetworking发布请求

Sam*_*net 8 iphone post json afnetworking

我正在使用AFNetworking并创建一个我需要json反馈的帖子请求.下面的代码有效,但我有两个主要问题; 我在哪里发布ActivityIndi​​cator Manager?第二个问题是这个代码是正确的,新的我与块混淆所以我真的想知道我是否正在做正确的事情以获得最佳性能,即使它有效.

    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");    


}    
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助:)

art*_*ovm 8

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

正如steveOhh所说,你应该[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]用来打开活动网络指标.它是一个单例,因此它不需要您手动alloc-init和release.至于另一个问题,我注意到你在块调用中缺少一些参数,你也可以这样做,这是更清晰的代码:

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
Run Code Online (Sandbox Code Playgroud)


小智 2

为什么不使用这个来代替呢?

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

因此不需要分配和初始化

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

问候, 史蒂夫0hh