2011-04-30 61 views
2

当我运行以下谷歌翻译API URL http://translate.google.com/translate_a/t?client=t&text=Hello&langpair=en|fr它返回正确的结果。Xcode中的Google Translate API:NSMutableURLRequest返回NULL,不是预期的结果

但是,当我尝试在Xcode中使用以下内容时,它返回(Null)。如果您能提供任何帮助或见解,我将不胜感激。

NSString *urlPath = [NSString stringWithFormat:@"/translate_a/t?client=t&text=%@&langpair=en|fr",@"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); 

回答

4

您的编码不正确。将其更改为NSASCIIStringEncoding。

我只是固定的代码并运行它在本地和它的工作就像一个魅力...

这里的结果

[[[“卓悦”,“你好”,“”,“” ]],[[ “感叹词”,[ “的Bonjour”, “萨吕”, “所有\ u00f4”, “天狮”]]], “EN”,[[ “的Bonjour”,[5],1,0, 1000,0,1,0]],[[ “你好”,4 ,,, “”],[ “你好”,5,[[ “的Bonjour”,1000,1,0]],[[0,5 ]],“你好”]] ,,, [],1]

+0

就是这样!非常感谢 (: – undaunted 2011-04-30 17:39:49