iOS判斷網(wǎng)絡(luò)請求超時(shí)的方法
本文介紹了iOS判斷網(wǎng)絡(luò)請求超時(shí)的方法,代碼具體如下:
+ (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 "請求超時(shí)。" UserInfo={ NSErrorFailingURLStringKey=http://123.56.109.92/refitcar/service.s?sn=, _kCFStreamErrorCodeKey=-2102, NSErrorFailingURLKey=http://123.56.109.92/refitcar/service.s?sn=, NSLocalizedDescription=請求超時(shí)。, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x167da8e0 { Error Domain=kCFErrorDomainCFNetwork Code=-1001 "請求超時(shí)。" UserInfo={ _kCFStreamErrorCodeKey=-2102, NSErrorFailingURLStringKey=http://123.56.109.92/refitcar/service.s?sn=, NSErrorFailingURLKey=http://123.56.109.92/refitcar/service.s?sn=, NSLocalizedDescription=請求超時(shí)。, _kCFStreamErrorDomainKey=4 } } }
可見:
po error.localizedDescription 請求超時(shí)。
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 判斷請求超時(shí)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS 屏幕適配方案實(shí)現(xiàn)縮放window的示例代碼
這篇文章主要介紹了IOS 屏幕適配方案實(shí)現(xiàn)縮放window的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04iOS實(shí)現(xiàn)無限循環(huán)圖片輪播器的封裝
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)無限循環(huán)圖片輪播器的封裝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03iOS實(shí)現(xiàn)抖音點(diǎn)贊動畫效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)抖音點(diǎn)贊動畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01iOS 底部按鈕和應(yīng)用圖標(biāo)顯示未讀消息(帶數(shù)字)
本文主要介紹了iOS 底部按鈕和應(yīng)用圖標(biāo)顯示未讀消息的相關(guān)知識。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04詳解Xcode 9 設(shè)置 iOS無線真機(jī)調(diào)試
本篇文章主要介紹了詳解Xcode 9 設(shè)置 iOS無線真機(jī)調(diào)試,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12