AFNetworking 使用中错误总结
2016-07-07 11:37
309 查看
AFNetworking 使用中错误总结
1、在pods里把AFNetworking(2.6.3)版本更新到AFNetworking(3.0.0)版本时出现以下错误
原因:项目中已有的
2、请求数据时出现以下错误无法获取数据
根据错误提示3840在网上搜了一下,找到这个,这个网址好像现在打开不了了,主要是添加了第1、2、5行的代码,信任服务器无效或过期的SSL证书,虽然有点反应。但是还是请求不来数据
这个问题出现了两次,两次用不同方式解决了,都不是什么技术性问题,但花费了很长时间,够冤的。
1)NSURLSessionUploadTask上传不了图片
由于贪图方便,修改了AFNetworking里AFURLResponseSerialization.m下的
看着是没有问题,但是AFURLResponseSerialization.m下有几处-(instancetype)init{},但并不是没处都能改的,改错地方就跟我一样出现问题了,后来只在’@implementation AFJSONResponseSerializer’下的-(instancetype)init{}改就没有问题了
2)后台换了API后,请求不了数据,错误显示说数据格式不对
换API前
换API后
之前是有域名的,之后就没有,看着就没什么不对的,如果觉得不对就错了
解决办法:在换API后面加上/?,
总结:
1、确认接口和参数没错
2、查看manager.responseSerializer = [AFHTTPResponseSerializer serializer];
和 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@”text/html”]; 有没有添加
3、.info.plist文件有没有添加
支持原创,转载请注明出处
1、在pods里把AFNetworking(2.6.3)版本更新到AFNetworking(3.0.0)版本时出现以下错误
- `AFNetworking (~> 3.0.0)` required by `Podfile` - `AFNetworking (~> 2.2)` required by `PAAImageView (1.1.0)`
原因:项目中已有的
PAAImageView (1.1.0)只能支持AFNetworking(2.6.3)版本,更高的版本就会有冲突,而上面错误显示写得是`AFNetworking (~> 2.2),而不是AFNetworking(2.6.3),这是因为AFNetworking(2.6.3)是我测试出来PAAImageView (1.1.0)所能支持的最高的版本
2、请求数据时出现以下错误无法获取数据
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
根据错误提示3840在网上搜了一下,找到这个,这个网址好像现在打开不了了,主要是添加了第1、2、5行的代码,信任服务器无效或过期的SSL证书,虽然有点反应。但是还是请求不来数据
AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init]; [securityPolicy setAllowInvalidCertificates:YES]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; [manager setSecurityPolicy:securityPolicy]; manager.responseSerializer = [AFHTTPResponseSerializer serializer];
这个问题出现了两次,两次用不同方式解决了,都不是什么技术性问题,但花费了很长时间,够冤的。
1)NSURLSessionUploadTask上传不了图片
由于贪图方便,修改了AFNetworking里AFURLResponseSerialization.m下的
- (instancetype)init { self = [super init]; if (!self) { return nil; } self.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", @"text/javascript", nil]; // self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil]; return self; }
看着是没有问题,但是AFURLResponseSerialization.m下有几处-(instancetype)init{},但并不是没处都能改的,改错地方就跟我一样出现问题了,后来只在’@implementation AFJSONResponseSerializer’下的-(instancetype)init{}改就没有问题了
2)后台换了API后,请求不了数据,错误显示说数据格式不对
// 设置基本请求信息 AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager]; manage.responseSerializer = [AFJSONResponseSerializer serializer]; manage.requestSerializer.timeoutInterval = 30; manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; NSLog(@"11111111111---------------------------111111111111111111111"); // 2. 发出post请求 [manage POST:BASE_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { // 2.1 成功以后,输出返回信息 NSLog(@"POST Request %@",responseObject); block(responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"post request error %@",[error localizedDescription]); failure(error); }];
换API前
#define BASE_URL @"http://api.*******.***"
换API后
#define BASE_URL @"http://***.***.***.75/api"
之前是有域名的,之后就没有,看着就没什么不对的,如果觉得不对就错了
解决办法:在换API后面加上/?,
#define BASE_URL @"http://***.***.***.75/api/?"
总结:
1、确认接口和参数没错
2、查看manager.responseSerializer = [AFHTTPResponseSerializer serializer];
和 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@”text/html”]; 有没有添加
3、.info.plist文件有没有添加
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
支持原创,转载请注明出处
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法