AFNetworkingErrorDomain 网络请求报错

本文解析了AFJSONRequestOperation在请求特定API时遇到的错误。主要原因是服务器返回的内容类型为text/html,而默认情况下AFJSONRequestOperation只接受text/json、application/json或text/javascript。文中提供了修改客户端设置以接受text/html的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用AFJSONRequestOperation时,遇到报错

2014-08-08 16:24:26.470 DownjoyGameNews[6210:90b]   320.000000, 568.000000 , 320.000000 548.000000 
2014-08-08 16:24:26.566 DownjoyGameNews[6210:90b]  erroe Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
    "text/javascript",
    "application/json",
    "text/json"
)}, got text/html" UserInfo=0x10a80b8a0 {NSLocalizedRecoverySuggestion={
  "title": "abc",
  "platform": 1,
  "id": 123,
  "image": "http://baidu.com"
}, NSErrorFailingURLKey=http://api.news.d.cn/AdvertByJson.ashx/?name=%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%8E%A8%E8%8D%90&platform=1, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x10a801d70> { URL: http://api.news.d.cn/AdvertByJson.ashx/?name=%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%8E%A8%E8%8D%90&platform=1 }, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x10aa0f110> { URL: http://api.news.d.cn/AdvertByJson.ashx/?name=%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%8E%A8%E8%8D%90&platform=1 } { status code: 200, headers {
    "Cache-Control" = private;
    "Content-Length" = 86;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Fri, 08 Aug 2014 08:27:42 GMT";
    Server = "Microsoft-IIS/6.0";
    "X-AspNet-Version" = "2.0.50727";
    "X-Powered-By" = "ASP.NET";
} }, NSLocalizedDescription=Expected content type {(
    "text/javascript",
    "application/json",
    "text/json"
)}, got text/html}

意思是Error Domain=AFNetworkingErrorDomain Code=-1016,这个主要是因为By default, AFJSONRequestOperation accepts only "text/json", "application/json" or "text/javascript" content-types from server, but you are getting "text/html".

所以,Fixing on server would be better, but you can also add "text/html" content type as acceptable in your app

[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值