2017-08-28 43 views
0

我的Android應用程序使用舊的微軟翻譯非常好,但我有 問題讓應用程序與新的認知服務工作。問題得到天藍色的翻譯與Android的工作

首先,我得到了具有下列值 新蔚藍翻譯帳戶(這些都不是真實值)

名稱:MYTranslateAcct

資源組:翻譯

訂閱ID:981h5ce7- 7ac7-4f6f-b4a5-ff04dc9e4266

key1的d122230418a8479ab5c06f2f1fca664c

key2 39c1f187o9814f4e983jba9eedd2e2c7

我的第一步是嘗試獲取令牌。微軟在文件中使用了 這個在JAVA中,但不是在Android環境中。我挖周圍,並有 放在一起的一些代碼,但它不工作。一個問題是, 文檔使用了我帳戶中沒有的術語,如「app-id」和 「key」。我沒有這些東西 - 我只有上面的值 的列表。

這是我的代碼。 。 。

class translateMessage extends AsyncTask<Void, Void, String> 
{ 
    String retString; 
    String inString = null; 

    translateMessage(String inString) { this.inString = inString; } 
    @Override 
    protected String doInBackground(Void... arg0) 
    { 
     try 
     { 


      String key = "881b5ce7-9ac7-4f6f-b4a5-ff04dc9e3199"; 
      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", key); 
      IOUtils.write("", authConn.getOutputStream(), "UTF-8");  //following line of code gets the exception . . . 
      String token = IOUtils.toString(authConn.getInputStream(), "UTF-8"); //this blows 
     } 
     catch (Exception e) 
     { 
      String myString = e.getMessage(); 
      String aString = "look at e"; 

     } 
     return retString; 
    } 

    @Override 
    protected void onPostExecute(String result) 
    { 
     String debugStr = result; 
     translation.setText(result); 
    } 
} 

以下是例外。 。 。

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

我在做什麼錯? 是否有人知道任何使用新服務的Android java代碼?

回答

0

我發佈了這個Azure支持,有人在一小時內給我打電話。我傳錯了鑰匙。當你獲得翻譯認知賬戶時,你會得到Key1和Key2。他們中的任何一個都會工作。所以代碼是一個有用的代碼示例,它將獲得令牌。