服务器之家

服务器之家 > 正文

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

时间:2021-02-22 14:11     来源/作者:DynastyTing

 本文介绍了iOS判断网络请求超时的方法,代码具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
+ (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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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  判断请求超时

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/dynastyting/article/details/51498350

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部