AFNetworking获得json值。网址不起作用

时间:2014-07-19 12:31:35

标签: ios json afnetworking afnetworking-2

我正在使用AFNetworking从我的iOS应用程序中获取JSON值我已将我的代码测试到此链接 http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?v=2&alt=json ,那么我的代码工作正常。但当我使用 http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php 此链接检索数据时,我会发现以下错误,请在下面找到我使用的代码。请帮助我从我的网址中检索数据

-(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);
}];

}

错误日志

  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}

5 个答案:

答案 0 :(得分:37)

它发生了

NSLocalizedDescription=Request failed: unacceptable content-type: text/html

格式Content-Type“=”text / html没有您的AFNetworking。

只需转到Serilization->AFURLResponseSerialization.m第215行,然后进行更改:

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

它会对你有用。

更新版本的代码可能在第223行

答案 1 :(得分:1)

Swift的解决方案:

它将接受大部分内容类型。

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>

答案 2 :(得分:0)

您使用的网址(http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php)会返回一个未接受的内容类型标头,而不是text / html,它应返回application / json。

答案 3 :(得分:0)

正如Otanaught所说,你使用的网址(http://www.fr20.wearedesigners.net/WADMac/tourism/fetchGuideListAndroid.php)会返回一个不被接受的内容类型标题,而不是text / html,它应该返回application / json。

如果您有权访问PHP Web服务文件,请将其放在任何显示代码之前:

header('Content-type: application/json');

答案 4 :(得分:0)

如果你使用 afnetwork 2.6.3 那么 只需去Serilization - &gt; AFURLResponseSerialization.m,第224行,并将其更改为:

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

它会对你有用。