我在我的Android应用程序中使用Microsoft Translation API,但是却得到了NULL值?

问题描述

我正在使用此代码通过Microsoft Translation API翻译我的单词或句子

 public class TranslateMicrosoft
 {
     String subscriptionKey = "my key";
     String url =  "https://api.cognitive.microsofttranslator.com//translate?api-version=3.0&to=de,it";

// Instantiates the OkHttpClient.
    OkHttpClient client = new OkHttpClient();
    // This function performs a POST request.
    public String Post() throws IOException {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType,"[\n{\"Text\":\"I would really like to drive your car around the block a few times.\"}\n]");
        Request request = new Request.Builder()
                .url(url).post(body)
                .addHeader("Ocp-Apim-Subscription-Key",subscriptionKey)
                .addHeader("Content-type","application/json").build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    // This function prettifies the json response.
    public static String prettify(String json_text) {
        JsonParser parser = new JsonParser();
        JsonElement json = parser.parse(json_text);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(json);
    }

}

现在我要在MainActivity.java中调用上述类

    try {
        TranslateMicrosoft translateRequest = new TranslateMicrosoft();
        String response = translateRequest.Post();

        Log.e("RESULT ",translateRequest.prettify(response));
        TextView t=(TextView)findViewById(R.id.textview_slogan1);
        t.setText("Reslt:"+translateRequest.prettify(response));

    } catch (Exception e) {
        Log.e("erro ",""+e.getMessage());
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)