2017-09-24 1 views
0

次のコードを1ヶ月間信頼性をもって動作させた後、数日前に確実に動作しなくなりました。約半分の時間は、それが適切に翻訳された文字列と、それは、次の2つのメッセージのいずれかを返します時間の残りの半分を返します。蒼白な認知翻訳サービスの問題

java.io.FileNotFoundException: https://api.cognitive.microsoft.com/sts/v1.0/issueToken

にjava.net.UnknownHostException:ホスト 「api.microsofttranslator.com」を解決することができません。この問題の始まりの時期は私の自由な紺碧の認知サービスの有効期限と一致したホスト名

に関連したアドレスは、しかし、私は移行しないアカウント昨日払っていた預金口座に引き上げられ、問題は続く。

どうしてですか?

static class translateMessageX extends AsyncTask<String, Void, String> 
{ 
    //input string array of 3 items 
    //[0]is the message to be translated 
    //[1]is the from language i.e. "english" 
    //[2]is the to language i.e. "spanish" 
    //[3]"echo" or "received" 
    String retString; 
    String inString = null; 
    String messageType = null; 
    String URLHolder = ""; //hold the URL here while we are translating the text 
    @Override 
    protected String doInBackground(String... params) 
    { 
     inString = params[0]; 
     String from = params[1]; 
     String to = params[2]; 
     messageType = params[3]; 
     int urlStart = inString.indexOf("http"); 
     if (!(urlStart == -1)) 
     { 
      URLHolder = inString.substring(urlStart); 
      inString = inString.substring(0, urlStart -1); 
     } 
     else 
     { 
      URLHolder = ""; 
     } 


     Integer mesChars = params[0].length(); 
     Integer tCharsLeft = GlobalStuff.getTranslationsFromSP(); 
     if (tCharsLeft > 0) 
     { 
      if (tCharsLeft < mesChars) //we charge for both 'echo' and 'received' translations 
      { 
       GlobalStuff.updateTranslationInventory(tCharsLeft * -1); 
      } 
      else 
      { 
       GlobalStuff.updateTranslationInventory(mesChars * -1); 
      } 
      GlobalStuff.notifyListeners(this, "#uui", "notused", "notused"); 
      try 
      { 

       Language fromLang = GlobalStuff.getLang(from); 
       Language toLang = GlobalStuff.getLang(to); 

       //retString = Translate.execute(inString, fromLang, toLang); 
       //String debugstr = "look at retStr"; 
       String authenticationUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"; 
       HttpsURLConnection authConn = (HttpsURLConnection) new URL(authenticationUrl).openConnection(); 
       authConn.setRequestMethod("POST"); 
       authConn.setDoOutput(true); 
       authConn.setRequestProperty("Ocp-Apim-Subscription-Key", GlobalStuff.translateKey); 
       IOUtils.write("", authConn.getOutputStream(), "UTF-8"); 
       String token = IOUtils.toString(authConn.getInputStream(), "UTF-8"); 
       System.out.println(token); 
       // Using the access token to build the appid for the request url 
       String appId = URLEncoder.encode("Bearer "+token, "UTF-8"); 
       String text = URLEncoder.encode(inString, "UTF-8"); 
       String translatorTextApiUrl = String.format("https://api.microsofttranslator.com/v2/http.svc/Translate?appid=%s&text=%s&from=%s&to=%s", appId, text, fromLang, toLang); 
       HttpsURLConnection translateConn = (HttpsURLConnection) new URL(translatorTextApiUrl).openConnection(); 
       translateConn.setRequestMethod("GET"); 
       translateConn.setRequestProperty("Accept", "application/xml"); 
       retString = IOUtils.toString(translateConn.getInputStream(), "UTF-8"); 
       String debug = "look at retString"; 
      } 

      catch (Exception e) 
      { 
       retString = e.toString(); 
      } 
     } 
     else 
     { 
      retString = "OUT OF TRANSLATION CREDITS - " + inString; 
     } 

     return retString; 

    } 

    @Override 
    protected void onPostExecute(String result) 
    { 
     //rest of logic should be here?? 

     String debug = "look at result"; 
     String answer = extractTranslation(result); 
    .. . . . 
+0

まだこの問題がありますか? –

答えて

0

ホストが見つかりませんでした。単純な接続エラーのようです。これらのホストは存在します。

あなたが直接api.microsofttranslator.comの呼び出しにキーを渡すことで、トークンサービスへの呼び出しを無効にすることができます の問題を発見していないホストの1を固定し、他ではないhttps://cognitive.uservoice.com/knowledgebase/articles/1815385-api-translator-text-speech-using-the-api-key

クライアントアプリケーションにキーを埋め込まないことをお勧めします。プロキシがクライアントとしてクライアントを安全に識別できる独自のプロキシサービスからトランスレータサービスを呼び出す方が安全です。

関連する問題