通过json反馈AFNetworking发布请求

时间:2011-10-03 01:03:36

标签: iphone json post 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");    


}    

非常感谢您的帮助:)

2 个答案:

答案 0 :(得分:8)

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

正如steveOhh所说,您应该使用[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]打开活动网络指示器。它是singleton,因此它不需要您手动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

答案 1 :(得分:2)

为什么不使用它呢?

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];

因此无需分配和初始化

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

此致 Steve0hh