AFNetworking3.0出现Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed:

在使用 AFNetworking3.0出现了 Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed:这样的问题。那我们如何来解决呢?

我们止需要加一句话     

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

例如:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
[manager POST:requestURLString parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
{
    DDLog(@"%@", responseObject);
    block(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"dsajkdaksldjklasjdklajsdla ==== %@",error);
    failureBlock();
}];

加上这句很多直接就在此尝试,可是发现后果跟严重了,那就是出现了闪退现象。报错信息如下:

 

-[__NSDictionaryI bytes]: unrecognized selector sent to instance 0x604000475f40

2018-04-29 17:40:58.494902+0800 WQLMVVM-Master[21585:833455] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI bytes]: unrecognized selector sent to instance 0x604000475f40'

*** First throw call stack:

 

这个说明是类型不一样导致报错,为什么会出现这个现象呢,原因是因为,block成功里面返回的就已经是字典了(NSDictionary

所以(responseObject)就已经是字典了,只需要直接使用就可以了,不需要通过

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

转成字典了

你可能感兴趣的:(苹果,OC,iOS)