Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Google translate v2 api返回非UTF-8字符_Java_Google App Engine_Encoding_Google Translate - Fatal编程技术网

Java Google translate v2 api返回非UTF-8字符

Java Google translate v2 api返回非UTF-8字符,java,google-app-engine,encoding,google-translate,Java,Google App Engine,Encoding,Google Translate,我正在尝试在我的应用程序引擎项目中使用Google Translate v2 api。然而,对于重音字符,其编码是混乱的[例如,单词“student”在法语中应该是“étudiants”,变成了“étudiants”]。这是我的密码 URL url = new URL( "https://www.googleapis.com/language/translate/v2?key=" + KEY + "&q=" + u

我正在尝试在我的应用程序引擎项目中使用Google Translate v2 api。然而,对于重音字符,其编码是混乱的[例如,单词“student”在法语中应该是“étudiants”,变成了“étudiants”]。这是我的密码

    URL url = new URL(
            "https://www.googleapis.com/language/translate/v2?key=" + KEY
                    + "&q=" + urlEncodedText + "&source=en&target="
                    + urlEncodedLang);
    try {
        InputStream googleStream = url.openStream();

        // make a new bufferred reader, by reading the page at the URL given
        // above
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                googleStream));

        // temp string that holds text line by line
        String line;

        // read the contents of the reader/the page by line, until there are
        // no lines left
        while ((line = reader.readLine()) != null) {
            // keep adding each line to totalText
            totalText = totalText + line + "\n";
        }
        // remember to always close the reader
        reader.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
在浏览器中键入相同的URL(Ubuntu上的Chrome)可以正常工作,并返回包含正确重音字符的JSON响应

我错过了什么?
谢谢

要确保它具有UTF-8编码,您必须使用:

BufferedReader reader = new BufferedReader(new InputStreamReader(googleStream, "UTF-8"));

在另一种情况下,它使用的是默认编码,可能是
ISO-8859-1

您也可以尝试使用它来为您解决问题。

感谢您提供了这个快速而简单的解决方案。