androidgoogle-translatenetworkonmainthread

Google Translate / Android Error


I'm just starting out with the Google Translation API Client Libraries, using SDK 26/min 22 in AS 3.0.1, following http://cloud.google.com/translate/docs/reference/libraries. I'm using their suggested code, after getting a key and installing the library using the gradle app file.

package com.example.xxx.simptrans;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

// Imports the Google Cloud client library
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Context context = getApplicationContext();

        Translate translate = TranslateOptions.getDefaultInstance().getService();

        // The text to translate
        String text = "Hello, world!";

        // Translates some text into Russian
        Translation translation =
                translate.translate(
                        text,
                        TranslateOption.sourceLanguage("en"),
                        TranslateOption.targetLanguage("ru"));

        System.out.printf("Text: %s%n", text);
        System.out.printf("Translation: %s%n", translation.getTranslatedText());
    }
}

When I run it, I get a Network Thread Exception:

Caused by: android.os.NetworkOnMainThreadException 
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)......

I have, included my AndroidManifest.xml-

<uses-permission android:name="android.permission.INTERNET" />

What's causing the error? Thanks.


Solution

  • try to create an AsyncTask class then execute it with the text you want to translate . Because the problem may be that you are trying to access network via main thread

    class Translate extends AsyncTask<String, Void, String> {
    
    
        protected String doInBackground(String... params) {
    
             String text = params[0]; //text to translate
             Translate translate = TranslateOptions.getDefaultInstance().getService();
             Translation translation =
                    translate.translate(
                        text,
                        TranslateOption.sourceLanguage("en"),
                        TranslateOption.targetLanguage("ru"));
    
    
    
             return translation.getTranslatedText();
        }
    
        //this method will run after doInBackground execution
        protected void onPostExecute(String result) {
         System.out.printf("Translation: %s%n", result);
        }
    
    
    }
    

    to execute this class :

    Translate translate = new Translate();
    String text = "Hello, world!";
    System.out.printf("Text: %s%n", text);
    translate.execute(text );