I am new to AFNetworking
. How can i execute the success and failure blocks of the following
NSMutableDictionary *rus = [[NSMutableDictionary alloc] init];
[rus setValue:@"1211" forKey:@"id"];
[rus setValue:@"33" forKey:@"man"];
[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"http://mysite.co/service.php" rus];
How to show the success or failure after executing the above code fragment.
requestWithMethod does not actually complete the call, it just returns you an NSMutableURLRequest. You need to take that request and make an AFHTTPRequestOperation. You can set a success/failure block on this object and then run the start method.
NSMutableDictionary *rus = [[NSMutableDictionary alloc] init];
[rus setValue:@"1211" forKey:@"id"];
[rus setValue:@"33" forKey:@"man"];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"http://mysite.co/service.php" parameters:rus];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Code for success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//Code for failure
}];
[operation start];
All that being said, are you using an outdated version of AFNetworking? The method you're referencing is deprecated.
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
error:(NSError *__autoreleasing *)error
Above is the correct method to use. You should pass in an error object.
Finally, an even easier way to accomplish what you're doing with AFNetworking 2.0 is below:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://mysite.co/service.php" parameters:rus success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Code for success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Code for failure
}];
Try this code:
NSMutableDictionary *rus = [[NSMutableDictionary alloc] init];
[rus setValue:@"1211" forKey:@"id"];
[rus setValue:@"33" forKey:@"man"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://mysite.co/service.php" parameters:rus success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://mysite.co/service.php" parameters:rus success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Code for success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Code for failure
}];
github's AFNetworking document
https://github.com/AFNetworking/AFNetworking