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=[AFJSONRequestSerializerserializer]; NSMutableURLRequest*request; if(block){ method=@"POST"; request=[requestSerializermultipartFormRequestWithMethod:methodURLString:urlparameters:aParamDataconstructingBodyWithBlock:blockerror:nil]; }else{ request=[requestSerializerrequestWithMethod:methodURLString:urlparameters:aParamDataerror:nil]; } AFHTTPRequestOperation*op=[[AFHTTPRequestOperationalloc]initWithRequest:request]; AFJSONResponseSerializer*responseSerializer=[AFJSONResponseSerializerserializer]; responseSerializer.removesKeysWithNullValues=YES; responseSerializer.acceptableContentTypes=[NSSetsetWithObject:@"text/html"]; op.responseSerializer=responseSerializer; __weakAFHTTPRequestOperation*weakOp=op; [opsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation*operation,idresponseObject){ if([responseObject[@"code"]integerValue]==0){ if(success){ //success(weakOp,aParamData,responseObject[@"list"]); success(weakOp,aParamData,responseObject); } }else{ NSLog(@"operationerrormsg=[%@]",responseObject[@"description"]); if(failure){ failure(weakOp,aParamData,[selferrorWithRet:responseObject]); } } }failure:^(AFHTTPRequestOperation*operation,NSError*error){ NSLog(@"operationfailed=[%@]error=[%@]",operation,error); if(failure){ failure(weakOp,aParamData,error); } }]; returnop; }
打印error
ErrorDomain=NSURLErrorDomainCode=-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{ ErrorDomain=kCFErrorDomainCFNetworkCode=-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 } } }
可见:
poerror.localizedDescription 请求超时。
poerror.code -1001
poerror.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="ErrorDomain=kCFErrorDomainCFNetworkCode=-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 判断请求超时
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。