Created
July 14, 2013 15:56
-
-
Save katopz/5994703 to your computer and use it in GitHub Desktop.
Will call WordNet for translate Thai to English via AFJSONRequestOperation as JSON
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
/* | |
// optional cache | |
// AFNetwork | |
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil]; | |
[NSURLCache setSharedURLCache:URLCache]; | |
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; | |
*/ | |
const NSString *_WORD_NET_URI = @"http://th.asianwordnet.org/services/dictionary/json/th2en/"; | |
-(void) requestWordNet : (NSString *) targetString | |
{ | |
// clean up new line at end of string | |
targetString = [targetString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
// escape URL encode | |
targetString = [targetString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
// merge cmd+args | |
NSString *fullURL = [NSString stringWithFormat:@"%@%@", _WORD_NET_URI, targetString]; | |
// NSString -> NSURL | |
NSURL *url = [NSURL URLWithString:fullURL]; | |
// NSURL -> NSURLRequest | |
NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
// for stupid server send json as html | |
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]]; | |
// create JSON operation | |
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request | |
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) | |
{ | |
NSLog(@"success : %@", JSON); | |
} | |
failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON) | |
{ | |
NSLog(@"failure : %@", [error localizedDescription]); | |
}]; | |
// start JSON operation | |
[operation start]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment