J'utilise AFNetworking et créer une demande de publication pour laquelle j'ai besoin de feedback json. Le code ci-dessous fonctionne mais j'ai deux questions principales; Où puis-je libérer le gestionnaire ActivityIndicator? La deuxième question est ce code correct, étant nouveau je suis confondu avec des blocs, donc je veux vraiment savoir si je le fais bien pour une performance optimale, même si cela fonctionne.Demande AFNetworking Post avec des commentaires json
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");
}
Merci beaucoup pour votre aide à l'avance :)
D'où vient le 'AFJSONRequestOperation operationWithRequest: sucess: finition:' méthode viens de? Je ne le vois pas dans l'API. –
@reakinator Il est en fait '+ JSONRequestOperationWithRequest: succès: échec:' Voir l'exemple [ici] (https://github.com/AFNetworking/AFNetworking#readme). – borisdiakur