我是Google Cloud API的新用户,而且我正在使用Google Cloud Translation API v2(r51)。我试图调用LanguagesResource.getName()来获取人类可读的语言名称来显示所有支持的语言,但我总是得到null值。你能帮我解决这个问题。谢谢。 这是我的代码:
public static void main(String[] args) {
try {
Translate translate = createTranslateService();
Translate.Languages.List request = translate.languages().list();
LanguagesListResponse response = request.execute();
List<LanguagesResource> list = response.getLanguages();
for(LanguagesResource item : list) {
System.out.println(item.getName());
}
}
catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
我通过调用对象请求的setTarget()方法修复了我的问题。代码是这样的:
public static void main(String[] args) {
try {
Translate translate = createTranslateService();
Translate.Languages.List request = translate.languages().list();
request.setTarget("en");
LanguagesListResponse response = request.execute();
List<LanguagesResource> list = response.getLanguages();
for(LanguagesResource item : list) {
System.out.println(item.getName());
}
}
catch (IOException e) {
e.printStackTrace();
}
}