云翻译器显示语言列表

时间:2020-03-25 21:52:55

标签: java android database asynchronous ibm-cloud

因此,我尝试使用IBM Cloud Translator的数据填充listView,但Java示例不是为以下的andriod制作的:

IamAuthenticator authenticator = new IamAuthenticator("{apikey}");
LanguageTranslator languageTranslator = new LanguageTranslator("2018-05-01", authenticator);
languageTranslator.setServiceUrl("{url}");

IdentifiableLanguages languages = languageTranslator.listIdentifiableLanguages()
  .execute().getResult();

System.out.println(languages);

您可以看到我在下面尝试过的内容,但找不到原因,无法填充列表视图,这似乎可以很好地完成背景工作: 需要可以使用的语言列表。 这是https://cloud.ibm.com/apidocs/language-translator/language-translator?code=java#list-identifiable-languages

上的参考链接
import androidx.appcompat.app.AppCompatActivity;
import com.ibm.cloud.sdk.core.security.Authenticator;
import com.ibm.cloud.sdk.core.security.IamAuthenticator;
import com.ibm.watson.language_translator.v3.LanguageTranslator;
import com.ibm.watson.language_translator.v3.model.IdentifiableLanguage;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {


    private LanguageTranslator  translationService;


    private TextView cv;
    private ListView cc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cv = (TextView) findViewById(R.id.hoho);
        cc = (ListView) findViewById(R.id.tv);

        translationService = initLanguageTranslatorService();

    }
    private LanguageTranslator initLanguageTranslatorService() {

        Authenticator authenticator
                = new IamAuthenticator(getString(R.string.mb));

        LanguageTranslator service = new LanguageTranslator("2018-05-01", authenticator);
        service.setServiceUrl(getString(R.string.ab));

        return service;
    }

    private class showLang extends AsyncTask<String, Void, List<IdentifiableLanguage>> {

        @Override
        protected List<IdentifiableLanguage> doInBackground(String... strings) {

            List<IdentifiableLanguage> languages =
                    translationService.listIdentifiableLanguages()
                            .execute().getResult().getLanguages();
            List<IdentifiableLanguage> ne = languages;
            return  ne;
        }

        protected void onPostExecute(List<IdentifiableLanguage>  s) {
            super.onPostExecute(s);
            cc.setAdapter(s);
        }
    }

0 个答案:

没有答案