2014-04-29 10 views
3

我的应用程序中有几个Web服务调用。下面是我如何打电话给他们AFHTTPRequestOperation超时

+(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]; 
} 

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

这只发生在设备上。模拟器很好。此外,这不是一个无线网络问题。我完全失去了如何调试这个。

更新:所以我检查了服务器日志和电话没有按时到达。 我也试过使用Charles Proxy。最奇怪的事情发生了。代理打开时,超时不会发生。事实上,这些电话几乎立即发生。但是当我脱掉代理时,同样的问题会发生。

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

更新所以我最终将超时设置为150秒。现在,我的超时时间较少,但问题是,每当我发生超时时,控制台都会花费60秒。此外,超时代码是-1001

+1

什么例子VA提供loginParams和URLString?它使用代理但不工作没有代理?服务器能否将请求视为垃圾邮件请求?只是一个随机猜测。 – Ricky

+0

loginParams是字典:@ {@“userName”:@“simpleString”,@“password”:@“smipleString”,@“deviceName”:@“Somestring_anInt”}; –

+1

您设置的超时是在服务器端还是在NSMutableURLRequest上的timeoutInterval?如果是用于NSMutableURLRequest,那么150秒太长了。你可以截屏或粘贴超时错误日志吗? – Ricky

回答

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]; 
} 
} 

我希望这帮助。

+0

感谢您的回答。我会试试这个。所以这个问题导致2件事。第一个是等待很长时间,第二个是失败。这将避免失败,但我仍然期待避免等待。 –

+1

没问题。如果等待时间过长,则可以将timeoutInterval减少为10或5秒。这真的取决于你需要什么。如果你始终面对这个问题,我认为问题确实在连接或服务器端。不是你的前端代码的问题。 – Ricky

+0

也许您可以在尝试完代码后报告任何性能或问题,我会看到我可以进一步提供帮助。 – Ricky

1

//创建一个Twitter帐户 - (空)createTwitterAccountWithDictionary:(NSDictionary的*)字典 WithCompletionBlock:(无效(^)(用户* 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; 
} 
相关问题