如何在android studio中使用Yandex Translate api

时间:2017-04-26 11:20:26

标签: android android-studio android-gradle build.gradle

如何在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

2 个答案:

答案 0 :(得分:0)

您可以查看此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中的自述文件。

答案 1 :(得分:0)

您可以将此类添加到项目中并使用它:

#if os(macOS)
    // can't find way for MacOSX to request auth
#endif

#if os(watchOS) || os(tvOS)
    lm.requestWhenInUseAuthorization()
#endif

 #if os(iOS)
    lm.requestAlwaysAuthorization()
 #endif

更多细节: https://github.com/OlegTarashkevich/YandexTranslation