Reputation: 1
I need an example to convert Spanish language to English language using Google Translation API. I tried with the following code. It gives some exception. can someone help me on the same.
Code :
import com.google.api.translate.*;
public class GoogleTranslator {
/**
* @param args
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {// TODO code application logic here
try {
Translate.setHttpReferrer("sp-en");
String translatedText = Translate.translate("Hola mundo", Language.SPANISH, Language.ENGLISH);
System.out.println(translatedText);
} catch (Exception ex) {
ex.printStackTrace(); }
}
}
Error :
java.lang.Exception: [google-api-translate-java] Error retrieving translation.
at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:136)
at com.google.api.translate.Translate.execute(Translate.java:69)
at com.google.api.translate.Translate.translate(Translate.java:192)
at GoogleTranslator.main(GoogleTranslator.java:15)
Caused by: java.net.UnknownHostException: ajax.googleapis.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:115)
... 3 more
Upvotes: 0
Views: 2363
Reputation: 527
you don't have an api key, for example GoogleAPI.setKey(); you need something like this to continue i guess
Upvotes: 0
Reputation: 240996
Set your DNS to google one
if on linux machine
sudo gedit /etc/resolv.conf
add
nameserver 8.8.8.8
nameserver 8.8.4.4
or if on windows goto network setting and set DNS for active connection
Upvotes: 0
Reputation: 69002
You need to figure out why you don't reach this host: ajax.googleapis.com
The Exception says:
java.net.UnknownHostException: ajax.googleapis.com
The javadoc says UnknownHostException is thrown to indicate that the IP address of a host could not be determined.
You should your DNS.
nslookup ajax.googleapis.com
Upvotes: 1