2

我是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];
}
4

2 回答 2

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];
});
于 2012-06-13T11:19:49.503 回答
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];
}
于 2012-06-13T12:45:54.467 回答