我在使用IBM Language Translator时遇到问题。我不断收到java.net.ConnectException。我的平台是Windows 10,并且正在使用Eclipse Photon。我已禁用防火墙和病毒防护软件。以下是我的代码:
import java.util.ArrayList;
import java.util.List;
import com.ibm.watson.developer_cloud.language_translator.v3.LanguageTranslator;
import com.ibm.watson.developer_cloud.language_translator.v3.model.TranslateOptions;
import com.ibm.watson.developer_cloud.language_translator.v3.model.Translation;
import com.ibm.watson.developer_cloud.language_translator.v3.model.TranslationResult;
import com.ibm.watson.developer_cloud.language_translator.v3.util.Language;
class IBMTest {
public static void main(String[] args) {
LanguageTranslator service = new LanguageTranslator("2018-05-01");
service.setApiKey("...apikey...");
String textToTranslate = "Αυτοκινητόδρομος";
List<String> text = new ArrayList<>();
text.add(textToTranslate);
TranslateOptions translateOptions = new TranslateOptions.Builder().text(text).source(Language.GREEK)
.target(Language.ENGLISH).build();
TranslationResult firstResult = service.translate(translateOptions).execute();
List<Translation> translations = firstResult.getTranslations();
if (translations.size() > 1) {
System.out.println("There are multiple translations:");
for (Translation nextTranslation : translations) {
System.out.println("- " + nextTranslation);
}
}
}
}
应用程序在以下位置引发异常:
TranslationResult firstResult = service.translate(translateOptions).execute();
例外情况如下:
Exception in thread "main" java.lang.RuntimeException: java.net.ConnectException: Failed to connect to gateway-a.watsonplatform.net/54.209.159.70:443
at com.ibm.watson.developer_cloud.service.WatsonService$WatsonServiceCall.execute(WatsonService.java:534)
at com.packt.javanlp.cookbook.chapter.IBM.main(IBM.java:50)
at com.packt.javanlp.cookbook.chapter.IBM.main(IBM.java:35)
Caused by: java.net.ConnectException: Failed to connect to gateway-a.watsonplatform.net/54.209.159.70:443
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:165)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at com.ibm.watson.developer_cloud.service.WatsonService$WatsonServiceCall.execute(WatsonService.java:531)
... 2 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.base/java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.base/java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.base/java.net.PlainSocketImpl.connect(Unknown Source)
at java.base/java.net.SocksSocketImpl.connect(Unknown Source)
at java.base/java.net.Socket.connect(Unknown Source)
at okhttp3.internal.platform.Platform.connectSocket(Platform.java:129)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:245)
... 20 more
答案 0 :(得分:1)
似乎不是java问题。我刚刚测试并尝试通过TCP连接到您的服务器:
telnet 54.209.159.70 443
.... connection timeout
我认为您应该检查设置-请参阅https://cloud.ibm.com/apidocs/language-translator