iOS 使用AFNetworking遇到错误 Error Domain=com.alamofire.error.serialization.response Code=-1016

本文介绍了解决iOS应用中使用AFNetworking时遇到的“unacceptable content-type: text/html”错误的方法。通过修改AFNetworking的源代码,允许接收HTML内容类型,解决了因内容类型不匹配导致的请求失败问题。

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

 
 

iOS 使用AFNetworking遇到错误 Request failed: unacceptable content-type: text/html 错误日志: Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f999be478d0> { URL: myUrlXXXXXX } { status code: 200, headers { ** "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";** ** Connection = "keep-alive";** ** "Content-Encoding" = gzip;** ** "Content-Type" = "text/html; charset=utf-8";** ** Date = "Tue, 24 Nov 2015 09:06:04 GMT";** ** Expires = "Thu, 19 Nov 1981 08:52:00 GMT";** ** Pragma = "no-cache";** ** Server = "nginx/1.1.19";** ** "Set-Cookie" = "PHPSESSID=qdvtek1k91oeva2u8fats39l93; path=/";** ** "Transfer-Encoding" = Identity;** ** "X-Powered-By" = "PHP/5.3.10-1ubuntu3.21";** } }, NSErrorFailingURLKey=http:URL: myUrlXXXXXX, com.alamofire.serialization.response.error.data=<7b226572 726f725f 636f6465 223a302c 22657272 6f725f6d 7367223a 22222c22 75706c6f 61645f75 726c223a 22687474 703a5c2f 5c2f7777 772e7467 7370792e 636f6d5c 2f6c6f6f 70657273 5c2f7365 72766572 5c2f6269 6e5c2f63 7573746f 6d65725c 2f75706c 6f61645c 2f373332 31746f75 7869616e 672e6a70 67227d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}


原因: 不可接受的内容类型 “text/html” 解决方案: AFJSONResponseSerializer.m中,222行左右 把 这句: self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil]; 修改为: self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

在进行网络请求时出现-1016 是因为只支持 text/json,application/json,text/javascript 你可以添加text/html 一劳永逸的方法是 在 AFURLResponseSerialization.h 里面搜索 self.acceptableContentTypes 然后 在里面 添加 @"text/html",@"text/plain" 这样就可以解决-1016的错误了 但是随之而来的是3840错误 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x9152780 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} 你会发现出现此错误 怎么办呢 添加如下语句 就可以解决问题了 manger.requestSerializer= [AFHTTPRequestSerializerserializer];

### iOS 应用审核指南 2.1.0 条款详解 苹果公司对于提交至App Store的应用程序有着严格的审查标准,其中Performance: App Completeness (性能:应用程序完整性)[^1] 是指确保应用在发布时具备完整的功能性和用户体验。具体来说: #### 功能完备性 应用程序应提供完整无误的功能体验,在任何情况下都不应该显示占位符文本、缺失的内容或不完善的状态。如果某些特性依赖于未来的更新,则这些部分不应出现在当前版本中[^1]。 #### 用户界面一致性 用户界面上不允许存在未完成的设计元素或是临时性的视觉效果。所有的交互控件都需正常工作并指向实际存在的页面或操作流程。此外,还应当避免出现空白屏幕或其他形式的加载错误提示信息[^1]。 #### 数据准确性与时效性 所有展示给用户的资料必须是最新的,并经过验证确认其真实性;过期的数据源链接以及无法访问的服务接口都会影响到这一项评估结果。因此开发者要特别留意第三方服务集成可能带来的风险因素[^1]。 #### 测试充分度 为了证明已满足上述各方面的要求,建议申请者进行全面详尽的质量检测过程,包括但不限于内部测试团队试用反馈收集、外部Beta版分发渠道获取真实环境下的表现数据等措施来提高通过率[^1]。 ```swift // 示例代码用于说明如何处理网络请求失败的情况以保持良好的用户体验 func fetchData(completionHandler:@escaping(Result<Data, Error>) -> Void){ let url = URL(string:"https://example.com/data")! URLSession.shared.dataTask(with:url){ data,response,error in DispatchQueue.main.async { if let error = error{ completionHandler(.failure(error)) }else if let httpResponse = response as? HTTPURLResponse, !(200...299).contains(httpResponse.statusCode), let data = data{ do{ let jsonError = try JSONSerialization.jsonObject(with:data,options:.allowFragments) print("HTTP Status Code:",httpResponse.statusCode,"JSON Response Body:",jsonError) // 自定义错误类型以便更好地向用户提供有意义的信息 struct APIError:Codable{ var message:String? } guard let apiError = try? JSONDecoder().decode(APIError.self,from:data) else{ completionHandler(.failure(NSError(domain:"",code:httpResponse.statusCode,userInfo:[NSLocalizedDescriptionKey:"Unexpected server response"]))) return } completionHandler(.failure(NSError(domain:"",code:httpResponse.statusCode,userInfo:[NSLocalizedDescriptionKey:apiError.message ?? "Unknown error"]))) }catch{ completionHandler(.failure(error)) } }else if let data = data{ completionHandler(.success(data)) }else{ completionHandler(.failure(NSError(domain:"",code:-1001,userInfo:[NSLocalizedDescriptionKey:"No Data Received"]))) } } }.resume() } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值