2012-11-22 77 views
1

我想在Android中創建翻譯應用程序,我使用Google Translate API,但它有錯誤,請幫我解決。這是我的代碼使用Google API在Android中進行翻譯

public void onClick(View v) { 
     // TODO Auto-generated method stub 
     String InputString; 
     String OutputString = null; 
     InputString = InputText.getText().toString(); 

     try { 

      Translate.setHttpReferrer("http://translate.google.com.vn/"); 
      OutputString = Translate.DEFAULT.execute(InputString, Language.ENGLISH, Language.VIETNAMESE); //////// 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
      OutputString = "Error"; 

     } 

     OutputText.setText(OutputString);    
    } 

這裏是我的清單

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.stthing" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="15" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

錯誤線Translate.setHttpReferrer( 「http://translate.google.com.vn/」);

The method setHttpReferrer(String) is undefined for the type Translate 
+0

你能在這裏發表您的Android清單文件?並且還會發生確切的錯誤消息。 – Dilberted

+0

我已更新清單和錯誤消息 – phucloi89

回答

3

我不確定您是否知道,但翻譯API不再免費。所以你需要設置API密鑰以及..

,而不是使用

Translate.setHttpReferrer("http://translate.google.com.vn/"); 

使用

GoogleAPI.setHttpReferrer("http://translate.google.com.vn/"); 
GoogleAPI.setKey(/* Enter your API key here */); 
+0

如何獲取API密鑰? – phucloi89

+0

您需要購買,因爲它不再是免費服務。 – Dilberted