- Synchronous
NSURL *url = [NSURL URLWithString:kJsonURL];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSError *error = nil;
NSURLResponse *res = nil;
NSData *json = [NSURLConnection sendSynchronousRequest:req returningResponse:&res error:&error];
NSInteger statusCode = ((NSHTTPURLResponse *) res).statusCode;
// if success
if (json && statusCode == 200) {
NSArray* data = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingAllowFragments error:nil] objectForKey:@"data"];
// if failure
} else {
}
- Asynchronous
NSURL *url = [NSURL URLWithString:kJsonURL];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[NSURLConnection sendAsynchronousRequest:req queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *res, NSData *json, NSError *error) {
NSInteger statusCode = ((NSHTTPURLResponse *) res).statusCode;
// if success
if (json && statusCode == 200) {
NSArray* data = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingAllowFragments error:nil] objectForKey:@"data"];
// if failure
} else {
}
}];