问题得到天蓝色的翻译与Android的工作

问题描述:

我的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代码?

我发布了这个Azure支持,有人在一小时内给我打电话。我传错了钥匙。当你获得翻译认知账户时,你会得到Key1和Key2。他们中的任何一个都会工作。所以代码是一个有用的代码示例,它将获得令牌。