我正在尝试从 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 已经包含损坏的变音符号。
有人对此有想法吗?
谢谢和最好的问候, 保罗