Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking response failure block is called instead of success block

AFNetworking response failure block is being called when I get status code 200. How can I make the success be called instead?

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://128.199.94.58/test/bt/client_token.php" parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         self.clientToken = responseObject[@"customerID"];
         NSLog(@"Client Token received.");
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // Handle failure communicating with your server
         NSLog(@"Client Token request failed.%@",operation.responseString);
         NSLog(@"error code %ld",(long)[operation.response statusCode]);
     }];
like image 886
Hardik Amal Avatar asked May 14 '15 12:05

Hardik Amal


2 Answers

Look at the value of error. It will tell you why the connection failed. "Failure" in this context has nothing to do with the status code. Returning "404" is still a "success." Failure means you were unable to complete the operation.

like image 96
Rob Napier Avatar answered Nov 15 '22 03:11

Rob Napier


use acceptableStatusCodes as follows:

 AFHTTPRequestOperationManager *manager =  [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [TimeoutAFJSONRequestSerializer serializer];
NSMutableIndexSet* codes = [[NSMutableIndexSet alloc] init];
[codes addIndex: 200];
manager.responseSerializer.acceptableStatusCodes = codes;

[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
 {


 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
 }];
like image 38
kernix Avatar answered Nov 15 '22 04:11

kernix