AFNetworking Post Request with json feedback
Asked Answered
E

2

8

I am using AFNetworking and creating a post request for which I require json feedback. The code below works however I have two main questions; where do I release the ActivityIndicator Manager? The second question is this code correct, being new I get confused with blocks so I really want to know if I am doing it right thing for optimum performance, even though it works.

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


}    

Thank you very much for your help in advance :)

Expiration answered 3/10, 2011 at 1:3 Comment(2)
Where does the AFJSONRequestOperation operationWithRequest:sucess:finish: method come from? I don't see it in the API.Poppyhead
@reakinator He actually ment + JSONRequestOperationWithRequest:success:failure: See example here.Deracinate
P
8

I know this question is a bit old, but I still wanted to contribute.

As steveOhh said, you should use [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES] to turn on the activity network indicator. It is a singleton, and hence it doesn't require you to manually alloc-init and release. As to the other question, I noticed you are missing some parameters in your block calls, also, you can do this, which is much cleaner code:

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
Paramagnetic answered 19/1, 2012 at 3:11 Comment(4)
Hello, i used your code but i got always into the failure block :( am i missing something?Zinovievsk
@Malek Check that your URL is correct, I recommend NSLogging the NSError from the failure block and see the output.Paramagnetic
@Malek If you still can't pinpoint the issue, open a new question with the output of that NSLog and I'll be glad to help you.Paramagnetic
@ArturoVM I'm using your code and I'm wandering where do I set the method as post? ThanksWaken
I
2

Why not use this instead?

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];

Hence there's no need to alloc and init

Can't say much on the other codes, just started out learning objective-C and AFNetworking.. :)

Regards, Steve0hh

Intercourse answered 9/10, 2011 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.