我的应用中有几个网络服务电话。这是我如何称呼他们的一个例子
+(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
答案 0 :(得分:2)
根据我使用Web服务和AFHTTPRequest的经验,请求超时主要与服务器/网络相关。如果您确认服务器没有任何问题,我认为您可以做的最好的事情是将请求发送到 timeoutInterval (例如:15秒)。接下来,如果您仍然收到请求超时错误,则可以尝试 N次的相同操作。
例如,您可以执行以下操作: -
通过3次重试调用该函数:
[ServerRequest yourMethodName:yourLoginDict with:3];
功能: -
+(void)yourMethodName:(NSDictionary *) loginParams with:(NSUInteger)ntimes {
if (ntimes <= 0) {
NSLog(@"Request failed after trying for N times");
}
else{
NSDictionary * postDictionary = loginParams;
NSError * error = nil;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString * urlString = [NSString stringWithFormat:@"%@",@"YOUR URL"];
//Give the Request 15 seconds to load
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15.0];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:jsonData];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:urlRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successful request");
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request Failed at %d: %@",ntimes,error);
//If the request failed, call again with ntimes-1
[self yourMethodName:loginParams with:ntimes-1];
}
];
[operation start];
}
}
我希望这会有所帮助。
答案 1 :(得分:1)
//创建Twitter帐户 - (无效)createTwitterAccountWithDictionary:(NSDictionary的*)字典 WithCompletionBlock:(void(^)(user * userObj,NSString * errorMessager))completionBlock {
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"];
}];
}
并在AFURLResponseSerialization.m中 做了这些改变
- (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;
}