Error while trying to access Google translate with NSURL

假装没事ソ 提交于 2019-12-08 08:56:12

问题


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.


回答1:


(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.



来源:https://stackoverflow.com/questions/7576103/error-while-trying-to-access-google-translate-with-nsurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!