AFNetworking getting json value. URL doesn't work

Rehan K picture Rehan K · Jul 19, 2014 · Viewed 10.3k times · Source

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}

Answer

VenushkaT picture VenushkaT · Jul 19, 2014

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.