我熟悉ASIHTTPRequest库,并尝试实现与服务器的连接。这是我的代码:
- (BOOL)IsEnteredDataCorrect {
__block NSString *password;
__block NSString *responseString;
NSString *url = [NSString stringWithFormat:@"%@/login/",SERVER_URL];
__block ASIHTTPRequest *loginRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.labelText = @"Please wait";
[loginRequest setCompletionBlock:^
{
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
responseString = [loginRequest responseString];
// NSLog(@"%@",responseString);
NSData *responseData = [loginRequest responseData];
NSError *err = nil;
NSDictionary *users = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&err];
password = [users objectForKey:idField.text];
}];
[loginRequest setFailedBlock:^
{
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
[delegate alertError:[loginRequest error]];
NSLog(@"failed block");
}];
[loginRequest startAsynchronous];
NSLog(@"request is over");
if ([password isEqualToString:passwordField.text])
return YES;
else
return NO;
}
服务器名称是假的,所以我希望调用setFailedBlock。但奇怪的是:MBProgressHUD被带走(只有在调用其中一个块时才有可能),但是不执行setFailedBlock中的代码。进一步的代码成功执行。好的,setFailedBlock不起作用 - 但MBProgressHUD应保留在屏幕上,它就消失了。任何人都可以解释我发生了什么事吗?
答案 0 :(得分:0)
如果您的失败块未运行但MBProgressHUD正在消失,那么您的完成块正在运行。
也许您的服务器名称不像您认为的那样虚假?注册了很多奇怪的域名......
此外,您的代码在这里:
[loginRequest startAsynchronous];
NSLog(@"request is over");
if ([password isEqualToString:passwordField.text])
return YES;
else
return NO;
你似乎有一个基本的不匹配 - 你正在使用异步请求,但似乎期待答案立即出现。