JSON Error

August 5, 2008    JSON Programming

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 powered by Disqus