AFNetworking errorCode -1016 解决方法
AFNetworking 默认是只能解析以下格式,如果需要支持data等格式,需要增加acceptableContentTypes
AFNetworking.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];//默认情况
解决方法:
①:使用AFHTTPRequestOperation的情况:
1 AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"url字符串"]]]; 2 //可行 3 op.responseSerializer.acceptableContentTypes = [op.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; 4 [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { 5 NSData *responData = operation.responseData; 6 NSDictionary *dict = [responData convertToJSON]; 7 NSLog(@"%@",dict); 8 9 } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) { 10 NSLog(@"%ld",(long)error.code); 11 }]; 12 13 [[NSOperationQueue mainQueue] addOperation:op];
②:使用AFHTTPRequestOperationManager的情况:
1 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 2 manager.responseSerializer =[AFHTTPResponseSerializer serializer]; 3 [manager GET:@"url字符串" 4 parameters:nil 5 success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) 6 { 7 //两个都可以 8 // NSData *responData = operation.responseData; 9 NSData *responData = responseObject; 10 NSDictionary *dict = [responData convertToJSON]; 11 NSLog(@"%@",dict); 12 } 13 failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) 14 { 15 16 NSLog(@"%ld",(long)error.code); 17 18 }];
③:如果采用YTKNetwork则不会出现这样的情况,因为在YTKNetwork中默认是可以解析
1 typedef NS_ENUM(NSInteger , YTKRequestSerializerType) { 2 YTKRequestSerializerTypeHTTP = 0, 3 YTKRequestSerializerTypeJSON, 4 };
如果要解析json则需要自己更改。
只是自己的解决方案,如果有错误,希望看到的人可以指出来,大家一起进步。