Created
June 22, 2012 01:37
-
-
Save arthurnn/2969703 to your computer and use it in GitHub Desktop.
Fetch Movies for ios5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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