可能这个问题是重复的,但我没有从旧问题中找到任何解决方案。
我想翻译语言,我使用了google-api-translate-java-0.97.jar
.
我使用下面的代码我得到了如下错误:
com.google.api.GoogleAPIException: java.lang.Exception: [google-api-translate-java] 检索翻译时出错。
Java代码:
public class MainActivity extends Activity {
EditText MyInputText;
Button MyTranslateButton;
TextView MyOutputText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyInputText = (EditText) findViewById(R.id.InputText);
MyTranslateButton = (Button) findViewById(R.id.TranslateButton);
MyOutputText = (TextView) findViewById(R.id.OutputText);
MyTranslateButton.setOnClickListener(MyTranslateButtonOnClickListener);
}
private Button.OnClickListener MyTranslateButtonOnClickListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String InputString;
String OutputString = null;
InputString = MyInputText.getText().toString();
try {
GoogleAPI.setHttpReferrer("http://www.google.com");
GoogleAPI.setKey("AIzaSyCr9C8xNq0uODvZMNH_0Jb7BZpBCZMagOo");
OutputString = Translate.DEFAULT. execute(InputString, Language.ENGLISH,
Language.FILIPINO);
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
ex.printStackTrace();
OutputString = "Error";
}
MyOutputText.setText(OutputString);
}
};
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"
/>
<EditText
android:id="@+id/InputText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/TranslateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Translate"
/>
<TextView
android:id="@+id/OutputText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>