Google Weather API 返回的变音符号未正确显示

发布于 2024-12-25 06:27:42 字数 1249 浏览 2 评论 0原文

我正在尝试从 Google Weather API 读取天气信息。

我的代码看起来与此类似:

            String googleWeatherUrl = "http://www.google.de/ig/api?weather=berlin&hl=de";
    InputStream in = null;
    String xmlString = "";
    String line = "";
    URL url = null;
    try {
        url = new URL(googleWeatherUrl);
        in = url.openStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, UTF_8));
        while ((line = bufferedReader.readLine()) != null) {
            xmlString += line;
        }
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } 

    DocumentBuilder builder = null;
    Document doc = null;
    try {
        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource source = new InputSource(new StringReader(xmlString));
        doc = builder.parse(source);

    } catch (ParserConfigurationException e) {} 
              catch (FactoryConfigurationError e) {} 
              catch (SAXException e) {} catch (IOException e) {}

基本上它的工作原理就像一个魅力,但是当返回的数据包含变音符号(ö,ü,ä,...)时,这些字符将无法正确显示。在 Eclipse 以及浏览器或相应的源代码中,它们显示为矩形(或类似奇怪的东西)。

实际上变量 xmlString 已经包含损坏的变音符号。

有人对此有什么想法吗?

谢谢并致以最诚挚的问候, 保罗

I'm trying to read weather information from the Google Weather API.

My Code looks similar to this:

            String googleWeatherUrl = "http://www.google.de/ig/api?weather=berlin&hl=de";
    InputStream in = null;
    String xmlString = "";
    String line = "";
    URL url = null;
    try {
        url = new URL(googleWeatherUrl);
        in = url.openStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, UTF_8));
        while ((line = bufferedReader.readLine()) != null) {
            xmlString += line;
        }
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } 

    DocumentBuilder builder = null;
    Document doc = null;
    try {
        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource source = new InputSource(new StringReader(xmlString));
        doc = builder.parse(source);

    } catch (ParserConfigurationException e) {} 
              catch (FactoryConfigurationError e) {} 
              catch (SAXException e) {} catch (IOException e) {}

Basically it works like a charme but when the returned data contains umlauts (ö,ü,ä,...) then those characters are not displayed properly. In Eclipse as well as in the browser or in the corresponding source code they are displayed as rectangles (or something similar strange).

Actually already the variable xmlString contains the corrupted umlauts.

Does anybody have an idea on that?

Thanks and best regards,
Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2025-01-01 06:27:42

欢迎来到字符编码的神奇世界。请将您的理智留在门边的架子上...

您很可能需要使用 source.setEncoding(encoding) 并为网页指定正确的字符编码 - 如果您幸运的话,编码实际上可能在标题中指定。

将输入流的编码更改为“Latin1”,如下所示:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, Charset.forName("Latin1")));

在我的计算机上测试时,这会返回正确的德语字符:

Welcome to the magical world of Character Encodings. Please leave your sanity on the rack by the door...

You most likely need to use source.setEncoding(encoding) and specify the correct character encoding for the web page - if you're lucky the encoding might actually be specified in the headers.

Change your inputstream's encoding to "Latin1" like so:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, Charset.forName("Latin1")));

This returns proper german characters when tested on my machine:

<current_conditions><condition data="Meistens bewölkt"/>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文