iOS判断网络请求超时的方法
2019/7/9 23:05:22
本文主要是介绍iOS判断网络请求超时的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文介绍了iOS判断网络请求超时的方法,代码具体如下:
+ (AFHTTPRequestOperation *)requestOperationWithUrl:(NSString *)url requetMethod:(NSString *)method paramData:(NSDictionary *)aParamData constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block success:(successBlock)success failure:(failureBlock)failure { AFHTTPRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; NSMutableURLRequest *request; if (block) { method = @"POST"; request = [requestSerializer multipartFormRequestWithMethod:method URLString:url parameters:aParamData constructingBodyWithBlock:block error:nil]; }else{ request = [requestSerializer requestWithMethod:method URLString:url parameters:aParamData error:nil]; } AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializer]; responseSerializer.removesKeysWithNullValues = YES; responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; op.responseSerializer = responseSerializer; __weak AFHTTPRequestOperation *weakOp = op; [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if ([responseObject[@"code"] integerValue] == 0) { if (success) { // success(weakOp, aParamData, responseObject[@"list"]); success(weakOp, aParamData, responseObject); } }else{ NSLog(@"operation error msg = [%@]", responseObject[@"description"]); if (failure) { failure(weakOp, aParamData, [self errorWithRet:responseObject]); } } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"operation failed = [%@] error = [%@]", operation, error); if (failure) { failure(weakOp, aParamData, error); } }]; return op; }
打印 error
Error Domain=NSURLErrorDomain Code=-1001 "请求超时。" UserInfo={ NSErrorFailingURLStringKey=http://123.56.109.92/refitcar/service.s?sn=, _kCFStreamErrorCodeKey=-2102, NSErrorFailingURLKey=http://123.56.109.92/refitcar/service.s?sn=, NSLocalizedDescription=请求超时。, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x167da8e0 { Error Domain=kCFErrorDomainCFNetwork Code=-1001 "请求超时。" UserInfo={ _kCFStreamErrorCodeKey=-2102, NSErrorFailingURLStringKey=http://123.56.109.92/refitcar/service.s?sn=, NSErrorFailingURLKey=http://123.56.109.92/refitcar/service.s?sn=, NSLocalizedDescription=请求超时。, _kCFStreamErrorDomainKey=4 } } }
可见:
po error.localizedDescription 请求超时。
po error.code -1001
po error.userInfo { NSErrorFailingURLKey = "http://123.56.109.92/refitcar/service.s?sn="; NSErrorFailingURLStringKey = "http://123.56.109.92/refitcar/service.s?sn="; NSLocalizedDescription = "\U8bf7\U6c42\U8d85\U65f6\U3002"; NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1001 \"\U8bf7\U6c42\U8d85\U65f6\U3002\" UserInfo={_kCFStreamErrorCodeKey=-2102, NSErrorFailingURLStringKey=http://123.56.109.92/refitcar/service.s?sn=, NSErrorFailingURLKey=http://123.56.109.92/refitcar/service.s?sn=, NSLocalizedDescription=\U8bf7\U6c42\U8d85\U65f6\U3002, _kCFStreamErrorDomainKey=4}"; "_kCFStreamErrorCodeKey" = "-2102"; "_kCFStreamErrorDomainKey" = 4; }
所以使用 error.code是否等于 -1001 判断请求超时
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于iOS判断网络请求超时的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-12Axios库资料:新手入门必读教程
- 2024-11-11Axios库项目实战:新手入门教程
- 2024-09-29Axios库教程:初学者必备指南
- 2024-08-29Axios库资料:新手入门指南与基本使用教程
- 2024-03-14system bios shadowed
- 2024-03-14gabios
- 2024-02-07iOS应用提交上架的最新流程
- 2024-02-06打包 iOS 的 IPA 文件
- 2023-12-07uniapp打包iOS应用并通过审核:代码混淆的终极解决方案 ?
- 2023-11-25uniapp IOS从打包到上架流程(详细简单) 原创