我正在尝试在我的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):
答案 0 :(得分: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
祝你好运^ _ ^