尝试在java spring中连接watson toneanalyser时出现java.net.ConnectException

时间:2018-02-14 04:30:40

标签: java watson

我使用watson toneanalyser来检测文本中的主音。我在android中得到了结果,但在java spring中,当我调用get Tone函数时,我得到以下错误。

错误记录

 HTTP Status 500 - Request processing failed; nested exception is java.lang.RuntimeException: java.net.ConnectException: Failed to connect to gateway.watsonplatform.net/169.48.66.222:443
 type Exception report

  message Request processing failed; nested exception is java.lang.RuntimeException: java.net.ConnectException: Failed to connect to gateway.watsonplatform.net/169.48.66.222:443

 description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.RuntimeException: java.net.ConnectException: Failed to connect to gateway.watsonplatform.net/169.48.66.222:443
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.lang.RuntimeException: java.net.ConnectException: Failed to connect to gateway.watsonplatform.net/169.48.66.222:443
com.ibm.watson.developer_cloud.service.WatsonService$1.execute(WatsonService.java:176)
com.futuro.textmining.controller.MyController.getDominantEmotion(MyController.java:55)
com.futuro.textmining.controller.MyController.loadIndexLogin(MyController.java:86)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)

我的代码

 private String getDominantEmotion(String text){
     final String VERSION_DATE = "2018-02-14";
     ToneAnalyzer toneAnalyzer = new ToneAnalyzer(VERSION_DATE);
 toneAnalyzer.setUsernameAndPassword("80ff86fc-1329-46c1-9b51-45dda8d987e9", "WnQUuZcjVfLP");

      ToneOptions options = new ToneOptions.Builder().text(text).sentences(false).build();
        DocumentAnalysis documentAnalysis = toneAnalyzer.tone(options).execute().getDocumentTone();
        List<ToneScore> toneScores = documentAnalysis.getToneCategories().get(0).getTones();

        double maxScore = toneScores.get(0).getScore();
        String dominantTone = toneScores.get(0).getToneId();
        for(ToneScore toneScore : toneScores){
            if(toneScore.getScore() > maxScore){
                maxScore = toneScore.getScore();
                dominantTone = toneScore.getToneName();
            }
      }

     return dominantTone;
 }

1 个答案:

答案 0 :(得分:0)

我通过关闭防火墙和防病毒软件解决了这个问题,这有助于访问API。可能对寻找相同

的人有用