iOS 开发,使用AFNetworking 怎么设置请求超时的时间

 我来答
小宝啊baby
2016-03-03 · TA获得超过140个赞
知道小有建树答主
回答量:386
采纳率:0%
帮助的人:320万
展开全部
  POST请求

  + (AFHTTPRequestOperation *)GETMethod:(NSString *)URLString

  parameters:(id)parameters

  success:(void (^)(AFHTTPRequestOperation *operation, id
responseObject))success

  failure:(void (^)(AFHTTPRequestOperation *operation, NSError
*error))failure {

  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager
manager];

  // 设置超时时间

  [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];

  manager.requestSerializer.timeoutInterval = 10.f;

  [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];

  AFHTTPRequestOperation *httpOperation = [manager GET:URLString

  parameters:parameters

  success:^(AFHTTPRequestOperation *operation, id responseObject) {

  if (success) {

  success(operation, responseObject);

  }

  }

  failure:^(AFHTTPRequestOperation *operation, NSError *error) {

  if (failure) {

  failure(operation, error);

  }

  }];

  return httpOperation;

  }

  GET请求

  + (AFHTTPRequestOperation *)POSTMethod:(NSString *)URLString

  parameters:(id)parameters

  success:(void (^)(AFHTTPRequestOperation *operation, id
responseObject))success

  failure:(void (^)(AFHTTPRequestOperation *operation, NSError
*error))failure {

  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager
manager];

  manager.requestSerializer = [AFJSONRequestSerializer serializer];

  manager.responseSerializer.acceptableContentTypes =
[manager.responseSerializer.acceptableContentTypes
setByAddingObject:@"text/html"];

  // 设置超时时间

  [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];

  manager.requestSerializer.timeoutInterval = 10.f;

  [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];

  AFHTTPRequestOperation *httpOperation = [manager POST:URLString

  parameters:parameters

  success:^(AFHTTPRequestOperation *operation, id responseObject) {

  if (success) {

  success(operation, responseObject);

  }

  }

  failure:^(AFHTTPRequestOperation *operation, NSError *error) {

  if (failure) {

  failure(operation, error);

  }

  }];

  return httpOperation;

  }

  其中,设置这么一句话即可,参考更多请关注扣丁学堂。。。。。。。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式