这是我的问题。如果我的应用程序无法加载我的远程JSON文件,如何显示错误?我在我的电脑上关闭了我的无线网络,并在模拟器中运行了该应用程序。它NSLogs应该显示的消息,如果有连接。我怎样才能解决这个问题?谢谢。如果我的JSON未加载,则显示UIAlertView?
这里是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *jsonStr = @"http://xxx.com/server.php?json=1";
NSURL *jsonURL = [NSURL URLWithString:jsonStr];
// NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
// NSError *jsonError = nil;
NSURLRequest *jsonLoaded = [NSURLRequest requestWithURL:jsonURL];
if(!jsonLoaded) {
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.title = @"Error!";
alertView.message = @"A server with the specified hostname could not be found.\n\nPlease check your internet connection.";
[alertView addButtonWithTitle:@"Ok"];
[alertView show];
[alertView release];
NSLog(@"No connection, JSON not loaded...");
}
else {
NSLog(@"JSON loaded and ready to process...");
}
}
谢谢,我现在就试试这个。我可以插入这段代码吗?根据NSUrl? – 1789040
这仍然不起作用。我的json位于外部URL。我的无线网络关闭了,它仍然说,当它不可能时json被加载。 – 1789040
你的代码现在可以工作,但它只是NSLogs ...它不显示alertview。 – 1789040