Skip to content

Instantly share code, notes, and snippets.

@katopz
Created July 14, 2013 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save katopz/5994703 to your computer and use it in GitHub Desktop.
Save katopz/5994703 to your computer and use it in GitHub Desktop.
Will call WordNet for translate Thai to English via AFJSONRequestOperation as JSON
/*
// 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