2017-09-24 2 views
0

다음 코드가 한 달 동안 안정적으로 작동하면 며칠 전 안정적으로 작동하지 않습니다. 약 반 시간이 제대로 번역 된 문자열은 다음과 같은 두 가지 메시지 중 하나가 반환하는 시간의 나머지 절반을 반환푸른인지 변환 관련 문제

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의 호출로 키를 전달하여 토큰 서비스에 대한 호출을 무효화 할 수 있습니다 문제를 찾을 수없는 호스트 중 하나를 수정하지만 다른하지 https://cognitive.uservoice.com/knowledgebase/articles/1815385-api-translator-text-speech-using-the-api-key

.

키를 클라이언트 응용 프로그램에 포함시키지 않는 것이 좋습니다. 프록시가 클라이언트를 클라이언트로 안전하게 식별 할 수있는 자신의 프록시 서비스에서 변환기 서비스를 호출하는 것이 더 안전합니다.