Java中的Translation API问题

时间:2017-10-03 09:07:48

标签: java jdeveloper

我正在尝试使用translate.googleapis.com将英语翻译成阿拉伯语。

它适用于所有字母,除了一个字母,它总是显示字母'ف'为' ?' 有什么建议吗?

private static String callUrlAndParseResult(String langFrom, String langTo, String word) throws Exception {
    String url =
        "https://translate.googleapis.com/translate_a/single?" + "client=gtx&" + "sl=" + langFrom + "&tl=" +
        langTo + "&dt=t&q=" + URLEncoder.encode(word, "UTF-8");
    URL obj = new URL(url);
    URLConnection con = obj.openConnection();
    con.setRequestProperty("User-Agent", "Mozilla/5.0");

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    return parseResult(response.toString());
}

private static String parseResult(String inputJson) throws Exception {
    /*
   * inputJson for word 'hello' translated to language Hindi from English-
   * [[["??????","hello",,,1]],,"en"]
   * We have to get '?????? ' from this json.
   */

    JSONArray jsonArray = new JSONArray(inputJson);
    JSONArray jsonArray2 = (JSONArray) jsonArray.get(0);
    JSONArray jsonArray3 = (JSONArray) jsonArray2.get(0);

    return jsonArray3.get(0).toString();
}

public static void main(String[] args) {
    try {
        String word = callUrlAndParseResult("en", "ar", "phone");
        System.out.println(new String(word.getBytes(), Charset.forName("UTF-8")));
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

我正在使用jdeveloper 12cR2

1 个答案:

答案 0 :(得分:1)

请注意,无论何时使用Reader,都会在字符集之间进行转换。如果你没有指定你的字符集,它将使用系统默认字符集来编码传入的字节流,如果传入的字节流实际上与你的系统不在同一个字符集中,你会遇到麻烦。

因此,建议在使用Reader时具体说明字符集。

所以你的代码应该如下所示。

private static String callUrlAndParseResult(String langFrom, String langTo, String word) throws Exception {
    String url =
        "https://translate.googleapis.com/translate_a/single?" + "client=gtx&" + "sl=" + langFrom + "&tl=" +
        langTo + "&dt=t&q=" + URLEncoder.encode(word, "UTF-8");
    URL obj = new URL(url);
    URLConnection con = obj.openConnection();
    con.setRequestProperty("User-Agent", "Mozilla/5.0");

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    return parseResult(response.toString());
}

private static String parseResult(String inputJson) throws Exception {
    /*
   * inputJson for word 'hello' translated to language Hindi from English-
   * [[["??????","hello",,,1]],,"en"]
   * We have to get '?????? ' from this json.
   */

    JSONArray jsonArray = new JSONArray(inputJson);
    JSONArray jsonArray2 = (JSONArray) jsonArray.get(0);
    JSONArray jsonArray3 = (JSONArray) jsonArray2.get(0);

    return jsonArray3.get(0).toString();
}

public static void main(String[] args) {
    try {
        String word = callUrlAndParseResult("en", "ar", "phone");
        System.out.println(word);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}