AFNetworking getting json value. URL doesn't work
10,336
Solution 1
it happens
NSLocalizedDescription=Request failed: unacceptable content-type: text/html
format Content-Type" = "text/html doesn't have your AFNetworking.
Simply go to Serilization->AFURLResponseSerialization.m
, line 215, and change it:
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
it will work you.
More recent versions may have the code on line 223.
Solution 2
Solution for Swift:
It will accept most of the content type.
let manager=AFHTTPRequestOperationManager()
manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments) as AFJSONResponseSerializer
manager.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer
manager.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json", "text/html", "text/plain", "text/json", "text/javascript", "audio/wav") as Set<NSObject>
Author by
Rehan K
Updated on July 26, 2022Comments
-
Rehan K almost 2 years
Im using AFNetworking to get JSON values from my iOS application i have tested my code to this link http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?v=2&alt=json then my code works fine. but when i use http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php this link to retrieve data im getting following Error please find below the code i used.please help me to retrieve data from my URL
-(void) retriveData { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager.requestSerializer setValue:@"text/html" forHTTPHeaderField:@"Content-type"]; [manager GET:@"http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; }
Error log
2014-07-19 18:36:01.107 WADTourisum[3000:60b] Reachability Flag Status: -R ------- networkStatusForFlags 2014-07-19 18:36:01.768 WADTourisum[3000:60b] Error: Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0xa05c4e0 {NSErrorFailingURLKey=http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php, com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x8d8ba40> { URL: http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php } { status code: 200, headers { Connection = "Keep-Alive"; "Content-Type" = "text/html"; Date = "Sat, 19 Jul 2014 13:05:32 GMT"; "Keep-Alive" = "timeout=5, max=100"; Server = "Apache/2.2.19 (Unix) mod_ssl/2.2.19 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635"; "Transfer-Encoding" = Identity; "X-Powered-By" = "PHP/5.2.17"; } }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
-
shripad20 over 9 yearsWorked for me too. Thank you.
-
Developer over 7 yearsI have done this and it worked too but will it affect any other response??