开发者

Google Translate API in Xcode: NSMutableURLRequest returning NULL, not expected result

开发者 https://www.devze.com 2023-03-01 23:56 出处:网络
when I run the following google translation API URL http://translate.google.com/translate_a/t?client=t&text=Hello&langpair=en|fr it returns the correct result.

when I run the following google translation API URL http://translate.google.com/translate_a/t?client=t&text=Hello&langpair=en|fr it returns the correct result.

However, when I try to use the following in Xcode it returns (Null). I would appreciate any help or insight you can provide.

NSString *urlPath = [NSString stringWithFormat:@"/translate_a/t?client=t&text=%@&langpair=en|fr",@开发者_如何学JAVA"Hello"];

NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:@"translate.google.com" path:urlPath];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"GET"];

NSURLResponse *response;
NSError *error;
NSData *data;
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

NSLog(@"Text: %@",result);


Your encoding is incorrect. change it to NSASCIIStringEncoding.

I just fixed your code and ran it locally and it worked like a charm...

here are the results

[[["Bonjour","Hello","",""]],[["interjection",["bonjour","salut","all\u00f4","tiens"]]],"en",,[["Bonjour",[5],1,0,1000,0,1,0]],[["Hello",4,,,""],["Hello",5,[["Bonjour",1000,1,0]],[[0,5]],"Hello"]],,,[],1]

0

精彩评论

暂无评论...
验证码 换一张
取 消