欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-16 10:22:46
 本文介绍了ios判断网络请求超时的方法,代码具体如下: + (afhttprequestoperation *)requestoperation...

 本文介绍了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  判断请求超时

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