Yandex API 翻译器不适用于 Android 应用程序
Posted
技术标签:
【中文标题】Yandex API 翻译器不适用于 Android 应用程序【英文标题】:Yandex API Translator not working on Android Aplication 【发布时间】:2016-08-11 16:27:31 【问题描述】:我正在尝试在我的 android 应用程序中使用 Yandex 翻译器,但它给出了错误。在 Java 应用程序(主要)中进行测试时,它可以完美运行。我的问题是什么?
主要代码:
import com.rmtheis.yandtran.language.Language;
import com.rmtheis.yandtran.translate.Translate;
import com.veryinteligent.jarvas.R;
import com.veryinteligent.jarvas.activity.WordCommands;
import android.app.Activity;
import android.os.Bundle;
public class YandexTranslator extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wordcommands);
Translate.setKey(WordCommands.myKey);
String term = "obrigado por me ajudar";
try
term = Translate.execute(term, Language.PORTUGUESE, Language.ENGLISH);
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(term);
import com.rmtheis.yandtran.language.Language;
import com.rmtheis.yandtran.translate.Translate;
public class TranslationTester
public static void main(String[] args)
// TODO Auto-generated method stub
Translate.setKey("trnsl.1.1.20160515T173032Z.cb8002677c95725e.93f4f996a1647d5d0c6e336758723a8bb1bb41b4");
String term = "abrir vá";
try
term = Translate.execute(term, Language.PORTUGUESE, Language.ENGLISH);
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(term);
错误:
08-11 16:10:35.333: I/dalvikvm(1152): Could not find method org.json.simple.JSONValue.parse, referenced from method com.rmtheis.yandtran.YandexTranslatorAPI.jsonObjValToStringArr
08-11 16:10:35.333: W/dalvikvm(1152): VFY: unable to resolve static method 15804: Lorg/json/simple/JSONValue;.parse (Ljava/lang/String;)Ljava/lang/Object;
08-11 16:10:35.333: D/dalvikvm(1152): VFY: replacing opcode 0x71 at 0x0000
08-11 16:10:35.333: D/dalvikvm(1152): VFY: dead code 0x0003-0015 in Lcom/rmtheis/yandtran/YandexTranslatorAPI;.jsonObjValToStringArr (Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;
08-11 16:10:35.333: I/dalvikvm(1152): Could not find method org.json.simple.JSONValue.parse, referenced from method com.rmtheis.yandtran.YandexTranslatorAPI.retrievePropString
08-11 16:10:35.333: W/dalvikvm(1152): VFY: unable to resolve static method 15804: Lorg/json/simple/JSONValue;.parse (Ljava/lang/String;)Ljava/lang/Object;
08-11 16:10:35.333: D/dalvikvm(1152): VFY: replacing opcode 0x71 at 0x0004
08-11 16:10:35.333: D/dalvikvm(1152): VFY: dead code 0x0007-0012 in Lcom/rmtheis/yandtran/YandexTranslatorAPI;.retrievePropString (Ljava/net/URL;Ljava/lang/String;)Ljava/lang/String;
08-11 16:10:35.333: W/System.err(1152): java.net.UnknownHostException: translate.yandex.net
08-11 16:10:35.333: W/System.err(1152): at java.net.InetAddress.lookupHostByName(InetAddress.java:497)
08-11 16:10:35.333: W/System.err(1152): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
08-11 16:10:35.333: W/System.err(1152): at java.net.InetAddress.getAllByName(InetAddress.java:256)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:406)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:387)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726)
08-11 16:10:35.333: W/System.err(1152): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:121)
08-11 16:10:35.333: W/System.err(1152): at com.rmtheis.yandtran.YandexTranslatorAPI.retrieveResponse(YandexTranslatorAPI.java:76)
08-11 16:10:35.333: W/System.err(1152): at com.rmtheis.yandtran.YandexTranslatorAPI.retrievePropArrString(YandexTranslatorAPI.java:104)
08-11 16:10:35.333: W/System.err(1152): at com.rmtheis.yandtran.translate.Translate.execute(Translate.java:52)
08-11 16:10:35.333: W/System.err(1152): at com.veryinteligent.jarvas.activity.YandexTranslator.onCreate(YandexTranslator.java:22)
08-11 16:10:35.333: W/System.err(1152): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-11 16:10:35.333: W/System.err(1152): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-11 16:10:35.333: W/System.err(1152): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-11 16:10:35.333: W/System.err(1152): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-11 16:10:35.333: W/System.err(1152): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-11 16:10:35.333: W/System.err(1152): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 16:10:35.333: W/System.err(1152): at android.os.Looper.loop(Looper.java:130)
08-11 16:10:35.333: W/System.err(1152): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-11 16:10:35.333: W/System.err(1152): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 16:10:35.333: W/System.err(1152): at java.lang.reflect.Method.invoke(Method.java:507)
08-11 16:10:35.333: W/System.err(1152): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-11 16:10:35.333: W/System.err(1152): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-11 16:10:35.333: W/System.err(1152): at dalvik.system.NativeStart.main(Native Method)
08-11 16:10:35.333: I/System.out(1152):
【问题讨论】:
改进了错误信息的格式;修正了一些措辞。 谢谢。我还是个初学者。 【参考方案1】:我使用此代码here,它对我来说很好用
1:将json_simple-1.1.jar添加到你的项目Tutorial
2:将类代码从yandex-translator-java-api/src/com/rmtheis/yandtran/
复制到您的项目中
3:将您的 API 密钥放入 ApiKeys.java
4:把这行翻译成一段文字:
Translate.setKey(ApiKeys.YANDEX_API_KEY);
String translatedText = Translate.execute("Hola, mundo!", Language.SPANISH, Language.ENGLISH);
5:如果您收到android.os.NetworkOnMainThreadException
错误,这是因为当应用程序尝试在其主线程上执行网络操作时抛出此异常,解决方案:
-- 在你的 YandexTranslatorApi.java
中添加这一行
static class RetrieveResponse extends AsyncTask<URL,Void,String>
@Override
protected String doInBackground(URL... params)
String result = "";
try
result = retrieveResponse(params[0]);
catch (Exception e)
e.printStackTrace();
return result;
-- 在retrievePropString
和retrievePropArrString
方法中更改这一行:
final String response = retrieveResponse(url);
通过这个:
final String response = new RetrieveResponse().execute(url).get();
已编辑:
将<uses-permission android:name="android.permission.INTERNET" />
添加到您的AndroidManifest.xml
祝你好运^_^
【讨论】:
以上是关于Yandex API 翻译器不适用于 Android 应用程序的主要内容,如果未能解决你的问题,请参考以下文章
Yandex Translator Api 使用到 vb.net