簡體   English   中英

如何在Android Studio中使用Yandex Translate API

[英]How do I use the Yandex Translate api in android studio

如何在Android Studio中使用Yandex Translate api。 我嘗試在https://github.com/vbauer/yandex-translate-api上使用此api,但下面出現錯誤

這是我的gradle文件。

    apply plugin: 'com.android.application'


android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.lecode.chatranslator"
        minSdkVersion 10
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
repositories {
    maven {
        url "https://jitpack.io"
    }
}

dependencies {
    compile ('com.github.vbauer:yandex-translate-api:1.2.5'){

    }
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    testCompile 'junit:junit:4.12'
    compile files('libs/json_simple-1.1.jar')
}

建立專案期間發生的錯誤

Information:Gradle tasks [:app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources]
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.2 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency commons-logging:commons-logging:1.2 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.2 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.json:json:20160212 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.2 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency commons-logging:commons-logging:1.2 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.2 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.json:json:20160212 is ignored for debug as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.2 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency commons-logging:commons-logging:1.2 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.2 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.json:json:20160212 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.2 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency commons-logging:commons-logging:1.2 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.2 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.json:json:20160212 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:Conflict with dependency 'com.google.code.findbugs:jsr305'. Resolved versions for app (3.0.1) and test app (2.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Error:Execution failed for task ':app:prepareDebugAndroidTestDependencies'.
> Dependency Error. See console for details.
Information:BUILD FAILED
Information:Total time: 7.412 secs
Information:1 error
Information:17 warnings
Information:See complete output in console

您可以檢查以下API: https//github.com/DoguD/Yandex-Translate-Android-API

只需將TranslatorBackgroundTask.java文件導入您的應用程序,然后執行如下所示:

import co.oriens.yandex_translate_android_api.TranslatorBackgroundTask;
import android.util.Log;

public class MainActivity extends Activity{
//Set context
Context context=this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Default variables for translation
    String textToBeTranslated = "Hello world, yeah I know it is stereotye.";
    String languagePair = "en-fr"; //English to French ("<source_language>-<target_language>")
    //Executing the translation function
    Translate(textToBeTranslated,languagePair);
}

//Function for calling executing the Translator Background Task
void Translate(String textToBeTranslated,String languagePair){
    TranslatorBackgroundTask translatorBackgroundTask= new TranslatorBackgroundTask(context);
    String translationResult = translatorBackgroundTask.execute(textToBeTranslated,languagePair); // Returns the translated text as a String
    Log.d("Translation Result",translationResult); // Logs the result in Android Monitor
}
}

有關更多詳細說明,您可以閱讀github中的自述文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM