首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AFHTTPRequestOperation超时

AFHTTPRequestOperation超时
EN

Stack Overflow用户
提问于 2014-04-29 17:41:43
回答 1查看 438关注 0票数 3

我的应用程序中有几个web服务调用。下面是我如何称呼他们的一个例子

代码语言:javascript
复制
+(void) login:(NSDictionary *) loginParams {

    AppDelegate *appDelegate  = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSString * URLString = [MPTLogin getLoginWSAddress];
    AFHTTPClient* client = [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:URLString]];
    NSMutableURLRequest *request = [client requestWithMethod:@"POST"
                                                    path:URLString
                                              parameters:loginParams];
    AFHTTPRequestOperation* operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"didSuccessfullyAttemptLogin" object:nil userInfo:nil];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if ([operation.response statusCode] == 401)
            [[NSNotificationCenter defaultCenter] postNotificationName:@"invalidUserNameOrPassword" object:self userInfo:nil];

        else
            [[NSNotificationCenter defaultCenter] postNotificationName:@"didFailedAttemptLogin" object:self userInfo:nil];
    }];

    [operation start];
}

问题是,如果在5-15分钟内没有调用web服务,则下一个web服务调用超时。然后在那之后的任何电话只需要一个实例的第一秒来完成,直到有几分钟没有电话。然后同样的事情再次发生。

这种情况只发生在设备上。模拟器没问题。而且,这不是一个无线网络问题。我完全不知道如何调试它。

更新:所以我检查了服务器日志,调用没有准时到达。我还尝试使用查尔斯代理。最奇怪的事情发生了。当代理打开时,超时不会发生。事实上,这些电话几乎马上就会发生。但是当我拿掉代理的时候,同样的问题就发生了。

更新:这不是一个wifi问题,因为我们在不同的设备,不同的wifi连接上,在不同的状态下尝试过。

更新,因此我最终将超时设置为150秒。现在,我有较少的超时,但问题是,每当我得到一个超时,控制台说它花了60秒。此外,超时代码是-1001。

EN

回答 1

Stack Overflow用户

发布于 2014-05-10 10:00:34

//创建Twitter帐户-(void)createTwitterAccountWithDictionary:(NSDictionary*)dictionary WithCompletionBlock:(void(^)(user *userObj,NSString *errorMessager))completionBlock{

代码语言:javascript
复制
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager] initWithBaseURL:BASE_URL];
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];

[manager POST:KCreateTwitterAccount parameters:dictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Request url:%@",operation.request.URL);
    NSLog(@"Response: %@", responseObject);
    if ([responseObject[@"status"]isEqualToString:@"SUCCESS"]) {
        user *userObj = [[user alloc]initWithUserDictionary:responseObject[@"data"]];
        completionBlock(userObj,nil);
        [SVProgressHUD showSuccessWithStatus:@"Success"];
    }
    else{
        [SVProgressHUD dismiss];
        completionBlock(nil,responseObject[@"message"]);
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Request url:%@",operation.request.URL);
    NSLog(@"Error: %@", error);
    [SVProgressHUD showErrorWithStatus:@"Failure"];
}];

}

在AFURLResponseerialization.m这些变化

代码语言:javascript
复制
- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }
#pragma mark - did changes
    //add this @"text/html"
    self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

    return self;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23371645

复制
相关文章

相似问题

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