在谷歌搜索了几个小时后,我仍然无法找到关于这个问题的单一话题:
1.如果用户没有互联网连接,我需要设置错误处理功能
2.用户有互联网连接,但无法连接到服务器。
我在哪里可以放这些?我尝试了一个主题,但它没有显示警报视图消息。
这是我的代码:
- (IBAction)getDataPressed
{
if([myRequest_ isExecuting])
{
return;
}
if(myRequest_ != nil)
{
[myRequest_ release];
}
myRequest_ = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:URL_PATH]];
myRequest_.delegate = self;
[myRequest_ startAsynchronous];
}
#pragma ASI Delegate methods
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"Request finished successfully");
NSLog(@"%@",[request responseString]);
NSDictionary *responseDictionary = [[request responseString]JSONValue];
NSDictionary *arrayElement = [responseDictionary objectForKey:@"user"];
NSString *ID = [arrayElement valueForKeyPath:@"id"];
NSLog(@"id: %@",ID);
NSString *usr = [arrayElement valueForKeyPath:@"usr"];
NSLog(@"usr: %@",usr);
NSString *gd = [arrayElement valueForKeyPath:@"gd"];
NSLog(@"gd: %@",gd);
NSString *ag = [arrayElement valueForKeyPath:@"ag"];
NSLog(@"ag: %@",ag);
NSString *st = [arrayElement valueForKeyPath:@"st"];
NSLog(@"st: %@",st);
NSString *lf = [arrayElement valueForKeyPath:@"lf"];
NSLog(@"lf: %@",lf);
NSString *da = [arrayElement valueForKeyPath:@"da"];
NSLog(@"da: %d",da);
for(NSString *value in [arrayElement allValues]){
NSLog(@"Found Value %@",value);
label.text = [value stringByAppendingFormat:@",%@,%@,%@,%@,%@,%@,",ID,usr,gd,ag,st,lf];
}
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"Error %@", [request error]);
if([request error])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Destination Unreachable" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
-(void) dealloc
{
[super dealloc];
}
感谢那些愿意帮助的人^ _ ^
答案 0 :(得分:0)
我认为您可以尝试使用Reachbility应用程序来帮助您检查是否有互联网。至于服务器本身,您可以使用NSUrlConnection Delegate方法检查您的请求是否存在问题(通过查看出现的HTTP代码类型)