我想使用google translate api将一些单词翻译成另一种语言为此我在代码中添加了translate.jar文件,但它总是给出null
public class Translator extends Activity {
/** Called when the activity is first created. */
String translatedText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
Translate t = new Translate() {
@Override
public String[] execute(String[] arg0, Language[] arg1, Language[] arg2)
throws GoogleAPIException {
Log.i("varun", "1");
// TODO Auto-generated method stub
return null;
}
@Override
public String[] execute(String arg0, Language arg1, Language[] arg2)
throws GoogleAPIException {
Log.i("varun", "2");
// TODO Auto-generated method stub
return null;
}
@Override
public String[] execute(String[] arg0, Language arg1, Language arg2)
throws GoogleAPIException {
Log.i("varun", "3");
// TODO Auto-generated method stub
return null;
}
@Override
public String execute(String arg0, Language arg1, Language arg2)
throws GoogleAPIException {
Log.i("varun", "translatedText"+translatedText);
// TODO Auto-generated method stub
return translatedText;
}
};
try {
translatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
//tv.setText(translatedText);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(this, translatedText, Toast.LENGTH_SHORT).show();
tv.setText(translatedText);
setContentView(tv);
}
}
如果有人有解决方案,它总是给null,请回复。 提前谢谢