尝试使用 NSURL 访问 Google 翻译时出错

发布于 2024-12-07 04:04:03 字数 507 浏览 0 评论 0原文

如果我尝试此代码:

NSError *err = nil;
NSString *resp = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=abdicate&langpair=en|es"]
                                          encoding:NSASCIIStringEncoding 
                                             error:&err];

Err 包含以下消息: Error Domain=NSCocoaErrorDomain Code=258 UserInfo=0x1001166d0 “文件名无效。”

有人知道这是什么意思吗?如果我在浏览器上尝试该网址,效果很好。

If I try this code:

NSError *err = nil;
NSString *resp = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=abdicate&langpair=en|es"]
                                          encoding:NSASCIIStringEncoding 
                                             error:&err];

Err contains the following message: Error Domain=NSCocoaErrorDomain Code=258 UserInfo=0x1001166d0 "The file name is invalid."

Anybody knows what this means? If I try the url on a browser, it works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-12-14 04:04:03

(使用 NSData)您只需将 1.0 替换为 1%2E0 并将 | 替换为 % 7C。句点可能被解释为带有扩展名的文件,因此出现 258 错误 (来自基础常量参考):

NS错误代码

Cocoa 错误域中的 NSError 代码。

enum {
   ...
   NSFileReadInvalidFileNameError = 258,
}

NSFileReadInvalidFileName错误:
由于文件无效而导致读取错误
name 适用于 Mac OS X v10.4 及更高版本。宣布于
FoundationErrors.h。

试试这个:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://ajax.googleapis.com/ajax/services/language/translate?v=1%2E0&q=abdicate&langpair=en%7Ces"]];
    if (data) {
        NSLog([[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
    }
}

给出输出:

2011-09-27 15:38:05.691 Project[44290:fb03] {"responseData": {"translatedText":"abdicar"}, "responseDetails": null, "responseStatus": 200}

这与您通过导航到网址 https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=abdicate&langpair=en|es 在浏览器中。


<罢工>
尝试使用 NSData,然后转换为字符串,如下所示:

NSString *result;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://ajax.googleapis.com/ajax/services/language/translate?%@", [self urlencode:@"v=1.0&q=abdicate&langpair=en|es"]]]];
    if (data) {
       result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    } else {
        // Do something
    }

这是上面使用的 urlencode 方法:

- (NSString *)urlencode:(NSString *)str {
    NSMutableString *output = [NSMutableString string];
    const unsigned char *source = (const unsigned char *)[str UTF8String];
    int sourceLen = strlen((const char *)source);
    for (int i = 0; i < sourceLen; ++i) {
        const unsigned char thisChar = source[i];
        if (thisChar == ' '){
            [output appendString:@"+"];
        } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || 
                   (thisChar >= 'a' && thisChar <= 'z') ||
                   (thisChar >= 'A' && thisChar <= 'Z') ||
                   (thisChar >= '0' && thisChar <= '9')) {
            [output appendFormat:@"%c", thisChar];
        } else {
            [output appendFormat:@"%%%02X", thisChar];
        }
    }
    return output;
}

这整个部分是不必要的,只需替换 1.0 和 | 即可。而不是对整个 url 进行编码。

(Using NSData) you can just replace the 1.0 with 1%2E0 and the | with %7C. It is possible that with the period is being interpreted as a file with an extension, thus the 258 error (From Foundation Constants Reference):

NSError Codes

NSError codes in the Cocoa error domain.

enum {
   ...
   NSFileReadInvalidFileNameError = 258,
}

NSFileReadInvalidFileNameError:
Read error because of an invalid file
name Available in Mac OS X v10.4 and later. Declared in
FoundationErrors.h.

Try this:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://ajax.googleapis.com/ajax/services/language/translate?v=1%2E0&q=abdicate&langpair=en%7Ces"]];
    if (data) {
        NSLog([[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
    }
}

Gives the output:

2011-09-27 15:38:05.691 Project[44290:fb03] {"responseData": {"translatedText":"abdicar"}, "responseDetails": null, "responseStatus": 200}

Which is the same as what you get by navigating to the url https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=abdicate&langpair=en|es in a browser.



Try using NSData, then converting to a string, like so:

NSString *result;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://ajax.googleapis.com/ajax/services/language/translate?%@", [self urlencode:@"v=1.0&q=abdicate&langpair=en|es"]]]];
    if (data) {
       result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    } else {
        // Do something
    }

This is the urlencode method used above:

- (NSString *)urlencode:(NSString *)str {
    NSMutableString *output = [NSMutableString string];
    const unsigned char *source = (const unsigned char *)[str UTF8String];
    int sourceLen = strlen((const char *)source);
    for (int i = 0; i < sourceLen; ++i) {
        const unsigned char thisChar = source[i];
        if (thisChar == ' '){
            [output appendString:@"+"];
        } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || 
                   (thisChar >= 'a' && thisChar <= 'z') ||
                   (thisChar >= 'A' && thisChar <= 'Z') ||
                   (thisChar >= '0' && thisChar <= '9')) {
            [output appendFormat:@"%c", thisChar];
        } else {
            [output appendFormat:@"%%%02X", thisChar];
        }
    }
    return output;
}

This whole part was unnecessary, just need to replace the 1.0 and | instead of encoding the entire url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文