AFNetworking getting json value. URL doesn't work
Asked Answered
M

5

8

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}
Moyna answered 19/7, 2014 at 12:31 Comment(0)
O
37

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.

Oren answered 19/7, 2014 at 17:22 Comment(1)
I have done this and it worked too but will it affect any other response??Braga
F
1

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>
Ferrante answered 6/11, 2015 at 15:50 Comment(0)
T
0

The url you use (http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php ) returns a not accepted content-type header, instead of text/html it should return application/json.

Trice answered 19/7, 2014 at 12:52 Comment(0)
T
0

As Otanaught says, the url you use (http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php ) returns a not accepted content-type header, instead of text/html it should return application/json.

If you have access to the PHP Web Service file, put this before any display code:

header('Content-type: application/json');
Tannin answered 21/9, 2014 at 21:38 Comment(0)
R
0

If u r using afnetwork 2.6.3 then Simply go to Serilization -> AFURLResponseSerialization.m, line 224, and change it to:

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

it will work you.

Reiner answered 23/1, 2018 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.