AFNetworking 使用中错误总结

来源:互联网 发布:java 版本 磁盘空间 编辑:程序博客网 时间:2024/05/09 22:13

AFNetworking 使用中错误总结
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>

支持原创,转载请注明出处

0 0