我的应用程序中有几个web服务调用。下面是我如何称呼他们的一个例子
+(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。
发布于 2014-05-10 10:00:34
//创建Twitter帐户-(void)createTwitterAccountWithDictionary:(NSDictionary*)dictionary 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"];
}];}
在AFURLResponseerialization.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;
}https://stackoverflow.com/questions/23371645
复制相似问题