i have an issue handling the JSON parsed data using AFNetworking.
here's my code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @{@"strEmail": _logInEmail.text, @"strPassword":_logInPassword.text};
[manager POST:@"http://carefid.com/api/user/login.php" parameters:parameters success:^
(AFHTTPRequestOperation *operation, id responseObject) {
//NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSString *str = responseObject[@"error"];
NSLog(@"JSON: %@", str);
UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"rootController"];
[self presentViewController:myController animated:YES completion:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
when im trying to log the value i get this error:
2014-07-15 14:39:08.438 carefid[7858:60b] -[_NSInlineData objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1094c67a0
2014-07-15 14:39:08.441 carefid[7858:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSInlineData objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1094c67a0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101d48495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001019c399e objc_exception_throw + 43
2 CoreFoundation 0x0000000101dd965d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000101d39d8d ___forwarding___ + 973
4 CoreFoundation 0x0000000101d39938 _CF_forwarding_prep_0 + 120
5 carefid 0x000000010001f14f __34-[ViewController existingUserBtn:]_block_invoke + 95
6 carefid 0x0000000100015648 __64-[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke46 + 40
7 libdispatch.dylib 0x0000000102669851 _dispatch_call_block_and_release + 12
8 libdispatch.dylib 0x000000010267c72d _dispatch_client_callout + 8
9 libdispatch.dylib 0x000000010266c3fc _dispatch_main_queue_callback_4CF + 354
10 CoreFoundation 0x0000000101da6289 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
11 CoreFoundation 0x0000000101cf3854 __CFRunLoopRun + 1764
12 CoreFoundation 0x0000000101cf2d83 CFRunLoopRunSpecific + 467
13 GraphicsServices 0x00000001040bef04 GSEventRunModal + 161
14 UIKit 0x0000000100570e33 UIApplicationMain + 1010
15 carefid 0x0000000100049183 main + 115
16 libdyld.dylib 0x00000001028cd5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
What could cause this problem? is the responseObject is an dictionary? if my code is wrong, how should I handle this?
You are setting the response parser to AFHTTPResponseSerializer
and not AFJSONResponseSerializer
. So the response is never parsed a JSON.
So change
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
to
manager.responseSerializer = [AFJSONResponseSerializer serializer];
As mentioned in you comment the server is delivering the content text/html
, if possible the server should say the content is op type application/json
, text/json
or text/javascript
.
If it is not possible to change the servers content type you can tell the AFJSONResponseSerializer
to accept text/html
like:
AFJSONResponseSerializer *jsonReponseSerializer = [AFJSONResponseSerializer serializer];
// This will make the AFJSONResponseSerializer accept any content type
jsonReponseSerializer.acceptableContentTypes = nil;
manager.responseSerializer = jsonReponseSerializer;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With