2

When using google api to translate the text I am getting the following error.

com.google.api.GoogleAPIException: java.lang.Exception: [google-api-translate-java] Error retrieving translation.
    at com.google.api.translate.TranslateV2.execute(TranslateV2.java:68)
    at GoogleTranslator.main(GoogleTranslator.java:16)
Caused by: java.lang.Exception: [google-api-translate-java] Error retrieving translation.
    at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:99)
    at com.google.api.translate.TranslateV2.execute(TranslateV2.java:62)
    ... 1 more
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://www.googleapis.com/language/translate/v2?key=AIzaSyAmtyPllqBCXu5rVikedZw8IErGCCfEdtw&q=How+are+you%3F&target=hi&source=en
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:93)
    ... 2 more
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://www.googleapis.com/language/translate/v2?key=AIzaSyAmtyPllqBCXu5rVikedZw8IErGCCfEdtw&q=How+are+you%3F&target=hi&source=en
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:89)
    ... 2 more

and my code is:

import com.google.api.GoogleAPI;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

    public class GoogleTranslator {

        /**
         * @param args
         *            the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            try {
                // Translate.setHttpReferrer(“en-fr”);
                GoogleAPI.setHttpReferrer("http://www.google.com");
                GoogleAPI.setKey("my key");
                String translatedText = Translate.DEFAULT.execute("How are you?",
                        Language.ENGLISH, Language.HINDI);
                System.out.println(translatedText);
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }

    }

Please need your help in resolving this.

4

0 回答 0