我正在使用 java Microsoft Translator API,但遇到了 Translator 卡住的问题;不翻译也不抛出任何异常。我曾尝试使用 Future 方法来检测超时,但即使这样也无济于事。代码无异常停止。这是我的代码。有什么问题吗?我能做些什么来解决它。
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
public class Translate
{
public static String translate (String word)
{
Translate.setClientId("My ID");
Translate.setClientSecret("My Secret");
ExecutorService executor = Executors.newCachedThreadPool();
Callable<String> task = new Callable<String>()
{
public String call()
{
try
{
String translatedWord = Translate.execute(currentWord, Language.FRENCH, Language.ENGLISH);
return translatedWord;
}
catch (Exception e)
{
return "I don't know";
}
}
};
Future<String> future = executor.submit(task);
try
{
String result = future.get(5, TimeUnit.SECONDS);
System.out.println(result);
return result;
}
catch (Exception e)
{
System.out.println("failed");
future.cancel(true);
return "I don't know";
}
}
public static void main (String args[])
{
String frenchWord = "Bonjour";
String englishWord = translate(frenchWord);
System.out.println(englishWord);
}
}
当我在调用方法中替换翻译行时,一切正常。但是, Translate.execute(...) 似乎导致了问题。