Skip to content

Instantly share code, notes, and snippets.

@arthurnn
Created June 22, 2012 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurnn/2969703 to your computer and use it in GitHub Desktop.
Save arthurnn/2969703 to your computer and use it in GitHub Desktop.
Fetch Movies for ios5
- (void)fetchMovies
{
__block NSString * url = @"http://sg.media-imdb.com/suggests/h/hello.json";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];
NSHTTPURLResponse *response;
NSError *error = nil;
NSData *moviesResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:moviesResponse waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseJSONP {
NSMutableString *resultString = [[NSMutableString alloc] initWithData:responseJSONP encoding:NSUTF8StringEncoding];
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"\\w+\\$\\w+?\\((\\{.*\\})\\)"
options:NSRegularExpressionCaseInsensitive
error:&error];
[regex replaceMatchesInString:resultString
options:0
range:NSMakeRange(0, [resultString length])
withTemplate:@"$1"];
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:[resultString dataUsingEncoding: NSUTF8StringEncoding] options:kNilOptions
error:&error];
if (error) {
NSLog(@"error: %@",error);
}
NSArray* movies = [json objectForKey:@"d"];
NSLog(@"movies: %@", movies);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment