AFNetworking response failure block is called instead of success block

The response to the AFNetworking failure response is triggered when I get a status code of 200. How can I call success 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]); }]; 
+5
source share
5 answers

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

+5
source

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) { }]; 
+2
source

I run this code and find work.

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:@"http://128.199.94.58/test/bt/client_token.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; self.clientToken = json[@"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]); }]; 

Answer:

 json: { customerID = "=="; } 

It may be work for you.

+1
source

If you check your error in the bounce block, clearly say that the invalid content type . You need to set the content type of the manager as follows

 manager.requestSerializer = [AFJSONRequestSerializer serializer]; 
0
source

try it

 manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 

and in the success block

  success:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:manager.responseData options:kNilOptions error:nil]; self.clientToken = dic[@"customerID"]; NSLog(@"Client Token received."); } 
0
source

All Articles