访问google天气预报API出现乱码

薪水 2010-10-21 09:07:52
提取GOOGLE天气代码如下:

/**
* @param latitude_e6 纬度
* @param longitude_e6 经度
* @return
*/
public Weather getWeather(String latitude_e6, String longitude_e6) throws RuntimeException {
// 获取google上的天气情况,写入文件
String fileAddr = System.getProperty("user.dir") + "/src/doc/" + latitude_e6 + "_" + longitude_e6 + ".xml";
Weather weather = null;

try {
//访问延迟
//Thread.sleep((int)(Math.random() * 10) * 200);

URL url = new URL("http://www.google.com/ig/api?hl=zh_cn&weather=,,," + latitude_e6 + "," + longitude_e6);
InputStream inputstream = url.openStream();

String s, str;
BufferedReader in = new BufferedReader(new InputStreamReader(inputstream));
StringBuffer stringbuffer = new StringBuffer();
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileAddr)));
while ((s = in.readLine()) != null) {
stringbuffer.append(s);
}
str = new String(stringbuffer);
out.write(str);
out.close();
in.close();

//解析XML文件得到天气信息
weather = parseWeather(fileAddr);
} catch (Exception e) {
e.printStackTrace();
Log log = Log.getLogger();
log.logger.error("网络连接失败或连接超时(connection timed out),无法连接到谷歌API!", e);
return null;
}

return weather;
}


红色部分是读取到数据写入本地文件,就是这个xml文件出现中文乱码,我做了很多转码,还是不行,特发此帖求解!
...全文
1010 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
adagio1983 2011-12-12
  • 打赏
  • 举报
回复
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource isr = new InputSource();
isr.setEncoding("GB2312");
isr.setByteStream(is);
L.e(PRE + "InputSource encoding:" + isr.getEncoding());
document = builder.parse(isr);
薪水 2010-10-22
  • 打赏
  • 举报
回复
BufferedReader in = new BufferedReader(new InputStreamReader(inputstream,"gb2312"));

可能google内部用的是gb2312编码吧,编码不一致导致乱码。
薪水 2010-10-21
  • 打赏
  • 举报
回复
xml文件:
<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data=""/><postal_code data=""/><latitude_e6 data="39930000"/><longitude_e6 data="116279998"/><forecast_date data="2010-10-21"/><current_date_time data="2010-10-21 15:00:00 +0000"/><unit_system data="SI"/></forecast_information><current_conditions><condition data="��"/><temp_f data="50"/><temp_c data="10"/><humidity data="ʪ�ȣ� 87%"/><icon data="/ig/images/weather/cn_overcast.gif"/><wind_condition data="���� ���������٣�1 ��/��"/></current_conditions><forecast_conditions><day_of_week data="����"/><low data="8"/><high data="15"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="������"/></forecast_conditions><forecast_conditions><day_of_week data="����"/><low data="10"/><high data="19"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="����Ϊ��"/></forecast_conditions><forecast_conditions><day_of_week data="����"/><low data="11"/><high data="20"/><icon data="/ig/images/weather/cn_cloudy.gif"/><condition data="����"/></forecast_conditions><forecast_conditions><day_of_week data="����"/><low data="5"/><high data="17"/><icon data="/ig/images/weather/chance_of_rain.gif"/><condition data="��������"/></forecast_conditions></weather></xml_api_reply>
qianyecq 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zouc816 的回复:]
qianyecq你运气真好!结贴
[/Quote]
求解决方法~
薪水 2010-10-21
  • 打赏
  • 举报
回复
qianyecq你运气真好!结贴
薪水 2010-10-21
  • 打赏
  • 举报
回复
谁来,满分送上
qianyecq 2010-10-21
  • 打赏
  • 举报
回复
怎么解决的求答案。
薪水 2010-10-21
  • 打赏
  • 举报
回复
问题已解决,没人来,这个咋办呢?

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧