Microsoft Translator API-为什么Translate.execute什么都没有返回?

时间:2017-02-06 22:46:20

标签: java android

我正在运行一个应用来测试我是否可以在运行Android API 19的手机上使用Microsoft Translator API。但是,当我按下按钮获取翻译文本时,而不是将文本“This is my dog”翻译成法语,Microsoft Translator API返回“”。

UPDATE :Logcat返回此信息:

02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err: java.lang.Exception: [microsoft-translator-api] Error retrieving translation : Permission denied (missing INTERNET permission?)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err:     at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err:     at com.memetix.mst.translate.Translate.execute(Translate.java:61)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err:     at com.example.inspiron.translat.MainActivity$1$1MyAsyncTask.doInBackground(MainActivity.java:45)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err:     at com.example.inspiron.translat.MainActivity$1$1MyAsyncTask.doInBackground(MainActivity.java:38)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-07 07:13:52.148 28001-28250/com.example.inspiron.translat W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-07 07:13:52.158 28001-28250/com.example.inspiron.translat W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-07 07:13:52.158 28001-28250/com.example.inspiron.translat W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-07 07:13:52.158 28001-28250/com.example.inspiron.translat W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at java.lang.Thread.run(Thread.java:841)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err: Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:390)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:343)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:289)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
02-07 07:13:52.168 28001-28250/com.example.inspiron.translat W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:     at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:133)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:     at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:     at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:  ... 9 more
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:     at libcore.io.Posix.getaddrinfo(Native Method)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:  ... 24 more
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err: Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
02-07 07:13:52.178 28001-28250/com.example.inspiron.translat W/System.err:  ... 27 more
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve virtual method 442: Landroid/content/Context;.getSystemService (Ljava/lang/Class;)Ljava/lang/Object;
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve virtual method 231: Landroid/app/Activity;.stopLockTask ()V
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.inspiron.translat.MainActivity.access$super
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve check-cast 227 (Landroid/os/PersistableBundle;) in Lcom/example/inspiron/translat/MainActivity;
02-07 07:15:59.518 29374-29374/com.example.inspiron.translat W/dalvikvm: VFY: unable to resolve virtual method 424: 

我的代码是:

public class MainActivity extends AppCompatActivity {
    TextView subject;
    Button submit;
    TextView result;
    String stayalive;
    String translatedText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        subject = (TextView) findViewById(R.id.textView);
        submit = (Button) findViewById(R.id.button);
        result = (TextView) findViewById(R.id.textView2);
        Translate.setClientId("CLIENT ID");
        Translate.setClientSecret("CLIENT SECRET");
        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {


                class MyAsyncTask extends AsyncTask<DownloadManager.Request, Void, String> {

                    protected String doInBackground() {
                        try {
                            translatedText = Translate.execute("This is my dog", Language.AUTO_DETECT, Language.FRENCH);
                            return "";

                        } catch (Exception e) {
                            e.printStackTrace();
                            return "";
                            // I can't do a void 
                        }
                    }

                    @Override
                    protected String doInBackground(DownloadManager.Request... params) {
                        return null;
                    }

                    @Override
                    protected void onPostExecute(String s) {

                        if (subject != null){
                            subject.setText(translatedText);
                            Context context = getApplicationContext();
                            CharSequence text = translatedText;
                            // the Toast didn't have any text in it 
                            int duration = Toast.LENGTH_SHORT;

                            Toast toast = Toast.makeText(context, text, duration);
                            toast.show();
                        } else {
                            Context context = getApplicationContext();
                            CharSequence text = "Hello toast!";
                            int duration = Toast.LENGTH_SHORT;
                            Toast toast = Toast.makeText(context, text, duration);
                            toast.show();
                        }
                    }
                }
                new MyAsyncTask().execute();




            }


        });


    }
}

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.inspiron.translat">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">

            <uses-permission android:name="android.permission.INTERNET"/>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

错误是因为我的代码还是因为Microsoft Translator API本身? (我正在使用来自https://code.google.com/archive/p/microsoft-translator-java-api/downloads的microsoft-translator-java-api-0.6.2-jar-with-dependencies.jar。)

更新2 :我从Permission denied (missing INTERNET permission?): But permission is given得到了答案。显然我应该将<uses-permission>标签放在application之上。

1 个答案:

答案 0 :(得分:0)

为什么你需要两个doInBackground调用?删除一个并尝试