Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google-api-translate-java Error retrieving translation

I am using

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv = new TextView(this);

     GoogleAPI.setHttpReferrer("http://code.google.com/p/google-api-translate-java/");
     GoogleAPI.setKey("AIzaSyBi7y4EgC0B-Q9UZg-tOzsOI0sLQ8F1Oow");

        try {
            String translatedText = Translate.DEFAULT.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
        } catch (GoogleAPIException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

I get the following error and I do not understand what am I doing wrong

02-26 10:53:53.409: W/System.err(2612): com.google.api.GoogleAPIException: java.lang.Exception: [google-api-translate-java] Error retrieving translation. 02-26 10:53:53.409: W/System.err(2612): at com.google.api.translate.TranslateV2.execute(TranslateV2.java:68) 02-26 10:53:53.409: W/System.err(2612): at com.example.testtranslate.Translator.onCreate(Translator.java:32) 02-26 10:53:53.409: W/System.err(2612): at android.app.Activity.performCreate(Activity.java:5158) 02-26 10:53:53.409: W/System.err(2612): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 02-26 10:53:53.409: W/System.err(2612): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 02-26 10:53:53.409: W/System.err(2612): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211) 02-26 10:53:53.410: W/System.err(2612): at android.app.ActivityThread.access$600(ActivityThread.java:149) 02-26 10:53:53.410: W/System.err(2612): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300) 02-26 10:53:53.411: W/System.err(2612): at android.os.Handler.dispatchMessage(Handler.java:99) 02-26 10:53:53.411: W/System.err(2612): at android.os.Looper.loop(Looper.java:153) 02-26 10:53:53.411: W/System.err(2612): at android.app.ActivityThread.main(ActivityThread.java:5086) 02-26 10:53:53.412: W/System.err(2612): at java.lang.reflect.Method.invokeNative(Native Method) 02-26 10:53:53.412: W/System.err(2612): at java.lang.reflect.Method.invoke(Method.java:511) 02-26 10:53:53.412: W/System.err(2612): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) 02-26 10:53:53.413: W/System.err(2612): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 02-26 10:53:53.413: W/System.err(2612): at dalvik.system.NativeStart.main(Native Method) 02-26 10:53:53.413: W/System.err(2612): Caused by: java.lang.Exception: [google-api-translate-java] Error retrieving translation. 02-26 10:53:53.414: W/System.err(2612): at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:99) 02-26 10:53:53.414: W/System.err(2612): at com.google.api.translate.TranslateV2.execute(TranslateV2.java:62) 02-26 10:53:53.416: W/System.err(2612): ... 15 more 02-26 10:53:53.416: W/System.err(2612): Caused by: java.lang.NullPointerException 02-26 10:53:53.418: W/System.err(2612): at libcore.net.http.HttpEngine.writeRequestHeaders(HttpEngine.java:647) 02-26 10:53:53.418: W/System.err(2612): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:801) 02-26 10:53:53.419: W/System.err(2612): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:293) 02-26 10:53:53.419: W/System.err(2612): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:185) 02-26 10:53:53.420: W/System.err(2612): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271) 02-26 10:53:53.420: W/System.err(2612): at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:93) 02-26 10:53:53.420: W/System.err(2612): ... 16 more

like image 552
Max Usanin Avatar asked Nov 03 '22 03:11

Max Usanin


1 Answers

The referrer needs to be set to the application URL from which you're sending the request.

https://developers.google.com/console/help/new/

This is almost always a access configuration problem. Try to see the response of the requested URL.. Response will be something like:

{ "error": { "errors": [ { "domain": "usageLimits", "reason": "accessNotConfigured", "message": "Access Not Configured. Please use Google Developers Console to activate the API for your project." } ], "code": 403, "message": "Access Not Configured. Please use Google Developers Console to activate the API for your project." } }

like image 184
Dropout Avatar answered Nov 14 '22 23:11

Dropout