在获取JSON数据时显示活动指示符

时间:2012-06-13 10:59:25

标签: ios json

我是IOS编程的初学者。我的问题是我的应用程序从我的Web服务器中的JSON获取数据,在启动应用程序时,由于获取过程而略有滞后和延迟,所以我想在连接到JSON数据时显示活动指示器。我怎么能这样做?

我的JSON编码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *urlAddress = [NSURL URLWithString:@"http://emercallsys.webege.com/RedBoxApp/getEvents.php"]; 
    NSStringEncoding *encoding = NULL;
    NSError *error;

    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:urlAddress usedEncoding:encoding error:&error];
    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];

    NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
    if (dict)
    {
        eventsDetail = [[dict objectForKey:@"eventsDetail"] retain];
    }

    [jsonreturn release];
}

2 个答案:

答案 0 :(得分:3)

使用以下代码

//add a UIActivityIndicatorView to your nib file and add an outlet to it
[indicator startAnimating];
indicator.hidesWhenStopped = YES;

dispatch_queue_t queue = dispatch_get_global_queue(
                                                   DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    //Load the json on another thread
    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:urlAddress usedEncoding:encoding error:NULL];
    [jsonreturn release];        
    //When json is loaded stop the indicator
    [indicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
});

答案 1 :(得分:1)

您可以使用以下代码:

- (void)fetchData
{
    [activityIndicator startAnimating];

    NSURL *url = [NSURL URLWithString:strUrl];

    NSURLRequest *theRequest = [[NSURLRequest alloc]initWithURL:url];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if(theConnection) {

    }
    else {
        NSLog(@"The Connection is NULL");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    webData = [[NSMutableData data] retain];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [webData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction %@",error);
    UIAlertView *connectionAlert = [[UIAlertView alloc] initWithTitle:@"Information !" message:@"Internet / Service Connection Error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [connectionAlert show];
    [connectionAlert release];

    [connection release];
    [webData release];

    return;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    //NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    //NSLog(@"Received data :%@",theXML);
    //[theXML release];

    NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary: webData error:&error];

    if (dict) {
        eventsDetail = [[dict objectForKey:@"eventsDetail"] retain];
    }

    [activityIndicator stopAnimating];
}