1

我正在编写一个简单的代码,其中我使用 Google API 进行翻译服务,但我收到以下错误:

Connection failed: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x176c80a0 {NSUnderlyingError=0x175c8d00 "bad URL", NSLocalizedDescription=bad URL}

这是我写的代码:

  #import "ViewController.h"
  #import"SBJson.h"

  @interface ViewController ()
  @property (strong, nonatomic) IBOutlet UITextField *textfield;
  @property (strong, nonatomic) IBOutlet UIButton *go;
  @property (strong, nonatomic) IBOutlet UITextView *textview;
- (IBAction)translate:(id)sender;

  @property (strong, nonatomic) NSMutableArray *translations;
  @property (strong, nonatomic)NSString *_lastText;
  @property (nonatomic, copy) NSString * lastText;
  @end

 @implementation ViewController
 @synthesize lastText = _lastText;

- (void)viewDidLoad
 {
 [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Initializing the translation array
 _translations = [[NSMutableArray alloc] init];
 }

 - (void)didReceiveMemoryWarning
 {
  [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
 }

- (void)performTranslation {

responseData = [[NSMutableData data] init ];

NSString *langString = @"en|ja";
NSString *textEscaped = [_lastText 
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    

NSString *langStringEscaped = [langString    
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

//My_Key=Google generated key
NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/language/translate/v2?
key={My_key}&source=en&target=de&q=%@",textEscaped];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

}
- (IBAction)translate:(id)sender {
[_translations removeAllObjects];
[_textfield resignFirstResponder];
_go.enabled=NO;
self.lastText = _textfield.text;
[_translations addObject:_lastText];
_textview.text = _lastText;

[self performTranslation];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
_textview.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
_go.enabled = YES;
 NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
 }

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSString *responseString = [[NSString alloc] initWithData:responseData   
encoding:NSUTF8StringEncoding];

NSMutableDictionary *luckyNumbers = [responseString JSONValue];
if (luckyNumbers != nil) {

    NSDecimalNumber * responseStatus = [luckyNumbers objectForKey:@"responseStatus"];
    if ([responseStatus intValue] != 200) {
        _go.enabled = YES;
        return;
    }

    NSMutableDictionary *responseDataDict = [luckyNumbers objectForKey:@"responseData"];
    if (responseDataDict != nil) {
        NSString *translatedText = [responseDataDict objectForKey:@"translatedText"];
        [_translations addObject:translatedText];
        self.lastText = translatedText;
        _textview.text = [_textview.text stringByAppendingFormat:@"\n%@", translatedText];
        _go.enabled = YES;
    }
}

}
@end
4

2 回答 2

1

问题就像它说的那样。URL 生成不正确。尝试:

NSString * source = @"en";
NSString * target = @"ja";
NSString * key = @"YOUR-KEY";

NSString * url = [NSString stringWithFormat:@"https://www.googleapis.com/language/translate/v2?key=%@&source=%@&target=%@",key,source,target];

看看你得到什么回应。

于 2014-07-18T14:54:29.673 回答
1

你可以只使用FGTranslator。简单的。

FGTranslator *translator = [[FGTranslator alloc] initWithGoogleAPIKey:@"your_google_key"];

[translator translateText:@"Bonjour!" 
               completion:^(NSError *error, NSString *translated, NSString *sourceLanguage)
{
    if (error)
        NSLog(@"translation failed with error: %@", error);
    else
        NSLog(@"translated from %@: %@", sourceLanguage, translated);
}];
于 2014-08-14T04:45:52.530 回答