java - 适用于 Android 的 Microsoft Translate API 出现 UnknownHostException

标签 java android language-translation microsoft-translator unknown-host

我已尝试使用以下代码来使用 Microsoft Translator API 库实现翻译应用程序,发现 here 。我有一个有效的互联网连接,并且已经生成了有效的客户端 ID 和密码,但每当我尝试运行代码时,我都会收到 java.net.UnknownHostException

我的代码:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
             StrictMode.setThreadPolicy(policy);
             Translate.setClientId("Screened for the question"); //Change this
               Translate.setClientSecret("I have screened the client secret to ask in SO"); //change

               final TextView  text = (TextView) findViewById(R.id.textView1);
               Button Trans1 = (Button)findViewById(R.id.trans);
               Trans1.setOnClickListener(new View.OnClickListener(){
               public void onClick(View view) {
                   String word = "Hello";
                   try {
                   translatedText = Translate.execute(word, Language.ENGLISH, Language.GERMAN);
                      // text.setText(translatedText);
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
               });

我的堆栈跟踪:

04-11 14:19:41.711: W/System.err(3084): java.lang.Exception: [microsoft-translator-api] Error retrieving translation : Unable to resolve host "datamarket.accesscontrol.windows.net": No address associated with hostname
04-11 14:19:41.711: W/System.err(3084):     at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202)
04-11 14:19:41.711: W/System.err(3084):     at com.memetix.mst.translate.Translate.execute(Translate.java:61)
04-11 14:19:41.711: W/System.err(3084):     at com.example.tanslator.TransActivity$1.onClick(TransActivity.java:45)
04-11 14:19:41.711: W/System.err(3084):     at android.view.View.performClick(View.java:3571)
04-11 14:19:41.711: W/System.err(3084):     at android.view.View$PerformClick.run(View.java:14247)
04-11 14:19:41.711: W/System.err(3084):     at android.os.Handler.handleCallback(Handler.java:605)
04-11 14:19:41.711: W/System.err(3084):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 14:19:41.711: W/System.err(3084):     at android.os.Looper.loop(Looper.java:137)
04-11 14:19:41.711: W/System.err(3084):     at android.app.ActivityThread.main(ActivityThread.java:4517)
04-11 14:19:41.711: W/System.err(3084):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 14:19:41.711: W/System.err(3084):     at java.lang.reflect.Method.invoke(Method.java:511)
04-11 14:19:41.711: W/System.err(3084):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
04-11 14:19:41.711: W/System.err(3084):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
04-11 14:19:41.711: W/System.err(3084):     at dalvik.system.NativeStart.main(Native Method)
04-11 14:19:41.711: W/System.err(3084): Caused by: java.net.UnknownHostException: Unable to resolve host "datamarket.accesscontrol.windows.net": No address associated with hostname
04-11 14:19:41.711: W/System.err(3084):     at java.net.InetAddress.lookupHostByName(InetAddress.java:400)
04-11 14:19:41.711: W/System.err(3084):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-11 14:19:41.711: W/System.err(3084):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-11 14:19:41.711: W/System.err(3084):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
04-11 14:19:41.711: W/System.err(3084):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-11 14:19:41.711: W/System.err(3084):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
04-11 14:19:41.711: W/System.err(3084):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
04-11 14:19:41.721: W/System.err(3084):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-11 14:19:41.721: W/System.err(3084):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
04-11 14:19:41.721: W/System.err(3084):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
04-11 14:19:41.721: W/System.err(3084):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:441)
04-11 14:19:41.721: W/System.err(3084):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
04-11 14:19:41.721: W/System.err(3084):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
04-11 14:19:41.721: W/System.err(3084):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
04-11 14:19:41.721: W/System.err(3084):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
04-11 14:19:41.721: W/System.err(3084):     at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:280)
04-11 14:19:41.721: W/System.err(3084):     at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:133)
04-11 14:19:41.721: W/System.err(3084):     at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160)
04-11 14:19:41.721: W/System.err(3084):     at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)
04-11 14:19:41.721: W/System.err(3084):     ... 13 more

最佳答案

我在使用这个 api 时也遇到了同样的问题,它在更改行后有效

translatedText = Translate.execute(word, Language.ENGLISH, Language.GERMAN);

translatedText = Translate.execute(word, Language.GERMAN);

关于java - 适用于 Android 的 Microsoft Translate API 出现 UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23008081/

相关文章:

java - 正则表达式非常慢

java - ProGuard 意外行为

java - 如何解析 freedict 文件(*.dict 和 *.index)

java - 如何从spark.sql.Row获取自定义对象

java - 如何检查我们现在是否位于两个日期时间对象之间?

android - 尝试摆脱华为 Ascend P6 上的持续日志警告调试

php - 没有原始文本弹出窗口的 Bing 翻译器小部件

python - 将 bash 前/后递增/递减转换为 python

java - Tomcat 和 spring-boot-starter

java - 如何在 Android 中运行后台任务