iOS判斷網路請求逾時的方法_IOS

來源:互聯網
上載者:User

 本文介紹了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  判斷請求逾時

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.