【发布时间】:2014-04-29 17:41:43
【问题描述】:
我的应用中有一些网络服务调用。这是我如何称呼它们的示例
+(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问题。我完全不知道如何调试它。
更新:所以我检查了服务器日志,呼叫没有按时到达。 我也尝试使用Charles Proxy。最离奇的事情发生了。当代理打开时,不会发生超时。事实上,电话几乎是立即发生的。但是当我取消代理时,会发生同样的问题。
更新:这不是 wifi 问题,因为我们已经在不同的设备、不同的 wifi 连接、不同的状态下进行了尝试。
更新所以我最终将超时设置为 150 秒。现在,我的超时时间减少了,但问题是每当我超时时,控制台都会说它需要 60 秒。超时代码也是-1001
【问题讨论】:
-
loginParams 和 URLString 的示例值是什么?它正在使用代理但不使用代理?服务器可以认为该请求是垃圾邮件请求吗?只是随机猜测。
-
loginParams 是字典:@{@"userName":@"simpleString",@"password":@"smipleString",@"deviceName":@"Somestring_anInt"};
-
你设置的超时时间是在服务器端还是 NSMutableURLRequest 上的 timeoutInterval?如果是 NSMutableURLRequest,150 秒太长了。你能截屏或粘贴超时错误日志吗?
标签: ios objective-c afnetworking nsoperation