JSON Error
If you want to show an error if the host is down it is pretty easy. This thinks you have read the other JSON posts. So look at the viewDidLoad function.
This is a continuation of Custom UITableViewCell
We are just adding a if {} else {} and if the data is nil, show an error.
- (void)viewDidLoad {
// Add the following line if you want the list to be editable
// self.navigationItem.leftBarButtonItem = self.editButtonItem;
// init the url
NSURL *jsonURL = [NSURL URLWithString:@"http://iphone.zcentric.com/test-json2.php"];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
if (jsonData == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Webservice Down" message:@"The webservice you are accessing is down. Please try again later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
else {
// converting the json data into an array
self.jsonArray = [jsonData JSONValue];
}
// releasing the vars now
[jsonURL release];
[jsonData release];
}
You can test this by simply changing the hostname to like iphone2.zcentric.com and it will show an error like the following.
Comments
2 Responses to “JSON Error”
Leave a Reply


Great website! Quick question: Is this line required?
[jsonURL release];
If I understand correctly (and I’m still learning here), Class Factory Methods return an object that is autoreleased. Is it then necessary to release it yourself?
Hey Chris, thank you for your question. The app I am working on was crashing when I tried to exit the app due to that line, but when I commented it out, it worked great.