AFNetworking-Post-Requests mit json-feedback

Ich bin mit AFNetworking und die Schaffung einer post-Anforderung, für die ich eine json-feedback. Der code unten funktioniert, allerdings habe ich zwei Fragen; wo kann ich lassen Sie die ActivityIndicator Manager? Die zweite Frage ist dieser code korrekt, wird neue bin ich verwirrt mit den Blöcken, so dass ich wirklich wollen, zu wissen, wenn ich bin, es zu tun richtige Ding für optimale Leistung, auch wenn es funktioniert.

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


}    

Danken Ihnen sehr für Ihre Hilfe im Voraus 🙂

  • Wo kommt die AFJSONRequestOperation operationWithRequest:sucess:finish: Methode her? Ich sehe es nicht in der API.
  • Er ist eigentlich ment + JSONRequestOperationWithRequest:success:failure: Siehe Beispiel hier.
InformationsquelleAutor Sam Barnet | 2011-10-03
Schreibe einen Kommentar