【问题标题】:Google Translate API in Xcode: NSMutableURLRequest returning NULL, not expected resultXcode 中的 Google 翻译 API:NSMutableURLRequest 返回 NULL,不是预期的结果
【发布时间】:2011-04-30 15:27:27
【问题描述】:

当我运行以下 google 翻译 API URL http://translate.google.com/translate_a/t?client=t&text=Hello&langpair=en|fr 时,它会返回正确的结果。

但是,当我尝试在 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);

【问题讨论】:

    标签: iphone google-translate


    【解决方案1】:

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

    我刚刚修复了你的代码并在本地运行它,它就像一个魅力......

    这是结果

    [[["Bonjour","Hello","",""]],[["感叹词",["bonjour","salut","all\u00f4","tiens"]]], "zh",,[["Bonjour",[5],1,0,1000,0,1,0]],[["你好",4,,,""],["你好",5, [["Bonjour",1000,1,0]],[[0,5]],"你好"]],,,[],1]

    【讨论】:

    • 就是这样!非常感谢(:
    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多