欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS判断网络请求超时的方法

时间:2021-04-17 09:53:59|栏目: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开发:一个音乐播放器的设计与实现案例

栏    目:iOS代码

下一篇:iOS开发之自定义UITextField的方法

本文标题:iOS判断网络请求超时的方法

本文地址:http://www.codeinn.net/misctech/103187.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有