首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用AFNetworking设置超时

如何使用AFNetworking设置超时
EN

Stack Overflow用户
提问于 2011-11-29 09:06:46
回答 3查看 69K关注 0票数 80

我的项目正在使用AFNetworking。

https://github.com/AFNetworking/AFNetworking

如何降低超时时间?没有互联网连接的自动取款机故障模块大约2分钟内不会被触发。Waay to long....

EN

回答 3

Stack Overflow用户

发布于 2014-07-17 05:52:04

您可以通过requestSerializer setTimeoutInterval method.You can requestSerializer从AFHTTPRequestOperationManager实例获取requestSerializer来设置超时间隔。

例如,执行一个超时为25秒的post请求:

代码语言:javascript
复制
    NSDictionary *params = @{@"par1": @"value1",
                         @"par2": @"value2"};

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager.requestSerializer setTimeoutInterval:25];  //Time out after 25 seconds

    [manager POST:@"URL" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

    //Success call back bock
    NSLog(@"Request completed with response: %@", responseObject);


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     //Failure callback block. This block may be called due to time out or any other failure reason
    }];
票数 28
EN

Stack Overflow用户

发布于 2013-03-14 04:19:19

基于其他人的回答和@mattt对相关项目问题的建议,如果你是AFHTTPClient的子类化,这里是一个快速的插件

代码语言:javascript
复制
@implementation SomeAPIClient // subclass of AFHTTPClient

// ...

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
  NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
  [request setTimeoutInterval:120];
  return request;
}

- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block {
  NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
  [request setTimeoutInterval:120];
  return request;
}

@end

经测试可在iOS 6上运行。

票数 5
EN

Stack Overflow用户

发布于 2013-09-30 00:12:56

我们不能用这样的计时器来做这个吗:

在.h文件中

代码语言:javascript
复制
{
NSInteger time;
AFJSONRequestOperation *operation;
}

在.m文件中

代码语言:javascript
复制
-(void)AFNetworkingmethod{

    time = 0;

    NSTtimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startTimer:) userInfo:nil repeats:YES];
    [timer fire];


    operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        [self operationDidFinishLoading:JSON];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        [self operationDidFailWithError:error];
    }];
    [operation setJSONReadingOptions:NSJSONReadingMutableContainers];
    [operation start];
}

-(void)startTimer:(NSTimer *)someTimer{
    if (time == 15&&![operation isFinished]) {
        time = 0;
        [operation invalidate];
        [operation cancel];
        NSLog(@"Timeout");
        return;
    }
    ++time;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8304560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档