I am trying to migrate a project from AFNetworking 1.3 to AFNetworking 2.0.
In the AFNetworking 1.3 project, I have this code:
- (void) downloadJson:(id)sender
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myServer/api/call?param1=string1¶m2=string2"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%ld", (long)[response statusCode]);
NSDictionary *data = JSON;
NSString *errorMsg = [data objectForKey:@"descriptiveErrorMessage"];
}];
[operation start];
}
When a client sends a URL that has not been properly formatted or with bad parameters, the server sends a 400 error back and turns on the JSON with "descriptiveErrorMessage", which I read in the bounce block. I use this "descriptiveErrorMessage" to determine what is wrong with the URL and tell the user if necessary.
The AFNetworking 2.0 project code is as follows:
- (void)downloadJson:(id)sender
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myServer/api/call?param1=string1¶m2=string2"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[operation start];
}
AFNetworking 2.0 JSON "descriptiveErrorMessage" , . NSHTTPURLResponse , , , , - .
JSON ? ?
.