尝试使用NSURL访问Google翻译时出错 [英] Error while trying to access Google translate with NSURL
问题描述
如果我尝试这个代码:
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文件名无效。
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.
推荐答案
(使用 NSData
),您可以用 1%2E0
和 |替换
与 1.0
%7C
。这可能是因为句点被解释为具有扩展名的文件,因此258错误( From Foundation Constants Reference ):
(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代码
NSError Codes
Cocoa错误网域中的NSError代码。
NSError codes in the Cocoa error domain.
enum {
...
NSFileReadInvalidFileNameError = 258,
}
b $ b
NSFileReadInvalidFileNameError:
由于文件无效而导致读取错误
name在Mac OS X v10.4及更高版本中可用。在
中声明。FoundationErrors.h。
NSFileReadInvalidFileNameError: Read error because of an invalid file name Available in Mac OS X v10.4 and later. Declared in 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,然后转换为字符串,如下所示:
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
}
是上面使用的urlencode方法:
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;
}
这整个部分是不必要的,只需要替换1.0和|而不是对整个网址进行编码。
This whole part was unnecessary, just need to replace the 1.0 and | instead of encoding the entire url.
这篇关于尝试使用NSURL访问Google翻译时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!