【问题标题】:AFNetworking getting json value. URL doesn't workAFNetworking 获取 json 值。网址不起作用
【发布时间】:2014-09-10 12:02:45
【问题描述】:

我正在使用 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 这个链接来检索我得到以下错误的数据时,请在我使用的代码下面找到。请帮助我从我的 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);
}];

}

错误日志

  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}

【问题讨论】:

    标签: ios json afnetworking afnetworking-2


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      它发生了

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

      format Content-Type" = "text/html 没有你的 AFNetworking。

      只需转到Serilization-&gt;AFURLResponseSerialization.m第 215 行,然后更改:

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

      它会对你有用。

      较新的版本可能在 第 223 行上有代码。

      【讨论】:

      • 也为我工作。谢谢。
      • 我已经这样做了,它也有效,但它会影响任何其他响应吗??
      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

        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>
        

        【讨论】:

          【解决方案5】:

          如果你使用 afnetwork 2.6.3 那么 只需转到 Serilization -> AFURLResponseSerialization.m,第 224 行,并将其更改为:

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

          它会对你有用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-17
            • 2015-03-26
            • 1970-01-01
            相关资源
            最近更新 更多