我使用这个简单的方法处理 ASIHTTP 请求失败。
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
if([error code] == ASIRequestTimedOutErrorType ) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error"
message:@"Connection Timed out"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
return;
} else if ( [error code] == ASIConnectionFailureErrorType) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error"
message:@"Connection Failiure"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
return;
}
}
您还可以在请求中设置连接超时,例如
[request_ setTimeOutSeconds:50.0];
这将等待 50 秒并显示连接超时错误。