解析的xml天气信息,怎么显示为中文?

ameyume
博客专家认证
2010-08-21 02:34:49
如图所示:我用http://www.google.com/ig/api?weather=Beijing查询的天气信息,在模拟器上自动显示全英文,怎么才能设置为显示中文呢?例如湿度,风向都显示汉字,不显示英语。
要在哪里设置编码呢?

还有一个问题,就是天气图标的gif文件,怎么才能显示出图标来呢?
路径设置为相对路径还是绝对路径?用什么控件好?
多谢!
...全文
2495 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
a554187160 2011-09-16
  • 打赏
  • 举报
回复
都是大牛
傲慢的上校 2011-04-24
  • 打赏
  • 举报
回复
学习而来,自己做的小项目也需要这个。
ameyume 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 yyy025025025 的回复:]
1. 通过firefox查看的,返回的信息开头为<xml_api_reply version="1">,没有指明charset的格式,没有head信息。
2. 标准的头格式为:
<html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312">
[/Quote]
多谢!
原来如此!
直接在ie中打开http://www.google.com/ig/api?weather=Beijing
返回的xml文件里也是
<?xml version="1.0" ?>
- <xml_api_reply version="1">
没有charset属性,一致没有注意到呢!
yyy025025025 2010-08-23
  • 打赏
  • 举报
回复
1. 通过firefox查看的,返回的信息开头为<xml_api_reply version="1">,没有指明charset的格式,没有head信息。
2. 标准的头格式为:
<html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312">
dinjay 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 yyy025025025 的回复:]
终于知道错在哪里了,google返回的html格式不是标准格式,所以需要定义解析文字的格式。
按照这个修改就OK了~~~

HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity, "utf-8");
InputStream is = new B……
[/Quote]
学习了
ameyume 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 yyy025025025 的回复:]
终于知道错在哪里了,google返回的html格式不是标准格式,所以需要定义解析文字的格式。
按照这个修改就OK了~~~

HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity, "utf-8");
InputStream is = new B……
[/Quote]
非常感谢版主!!!
版主是怎么解决的?怎么查看返回到是什么格式的编码?
还有google返回的html格式不标准是怎么看出来的?
yyy025025025 2010-08-23
  • 打赏
  • 举报
回复
终于知道错在哪里了,google返回的html格式不是标准格式,所以需要定义解析文字的格式。
按照这个修改就OK了~~~

HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity, "utf-8");
InputStream is = new ByteArrayInputStream(string.getBytes());
new DomXMLReader().readXML(is);
ameyume 2010-08-23
  • 打赏
  • 举报
回复
把查询的API再改为这个就可以显示成简体中文了,
http://www.google.com/ig/api?hl=zh-cn&weather=
再加上版主说的
HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity, "utf-8");
InputStream is = new ByteArrayInputStream(string.getBytes());
就OK了。
多谢各位的相助!!
ameyume 2010-08-23
  • 打赏
  • 举报
回复
应该和资源无关,我模拟器的语言是设置为简体中文的,在真机上测也不行
ameyume 2010-08-23
  • 打赏
  • 举报
回复
还是不行,
解析出来的还是US标准的
我增加了多语言的简体中文values-zh-rCN和美国英语values-en-rUS,只是多语言起作用

HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity, "utf-8").trim();
之前增加设置资源为简体中文的设置,即
			Resources resources = getResources();//获得res资源对象
Configuration config = resources.getConfiguration();//获得设置对象
DisplayMetrics dm = resources .getDisplayMetrics();//获得屏幕参数:主要是分辨率,像素等。
config.locale = Locale.SIMPLIFIED_CHINESE; //简体中文
resources.updateConfiguration(config, dm);

// HttpResponse response = httpClient.execute(httpGet, localContent);
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG, "Failed. status code is not ok.");
httpGet.abort();
} else {
// Get weather information successful
// Reset charset
HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity, "utf-8").trim();
InputStream is = new ByteArrayInputStream(string.getBytes("utf-8"));
return parseWeather(is);
}

取得string还是US标准的,取得的string如下所示
<?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="Beijing, Beijing"/>
<postal_code data="北京"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2010-08-23"/>
<current_date_time data="2010-08-24 02:00:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Cloudy"/>
<temp_f data="79"/>
<temp_c data="26"/>
<humidity data="Humidity: 68%"/>
<icon data="/ig/images/weather/cloudy.gif"/>
<wind_condition data="Wind: S at 4mph"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="Mon"/>
<low data="62"/>
<high data="89"/>
<icon data="/ig/images/weather/cloudy.gif"/>
<condition data="Cloudy"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Tue"/>
<low data="60"/>
<high data="84"/>
<icon data="/ig/images/weather/cloudy.gif"/>
<condition data="Cloudy"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Wed"/>
<low data="62"/>
<high data="82"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="Clear"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Thu"/>
<low data="62"/>
<high data="89"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="Clear"/>
</forecast_conditions>
</weather>
</xml_api_reply>
sike2008 2010-08-23
  • 打赏
  • 举报
回复
好东西 必须顶
jaysowen 2010-08-23
  • 打赏
  • 举报
回复
firebug使开发web程序必不可少的工具啊。。。呵呵。。。版主经验很丰富
ameyume 2010-08-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yyy025025025 的回复:]
new String(current.getCondition().getBytes(), "GB2312")

这句是没有用的,我发现所有的字串已经被转成了utf-8了,正在看是哪一步被转的。
[/Quote]
多谢。

使用这种形式http://www.google.com/ig/api?hl=zh-cn&weather=Beijing,查询出来的也还是乱码
yyy025025025 2010-08-22
  • 打赏
  • 举报
回复
new String(current.getCondition().getBytes(), "GB2312")

这句是没有用的,我发现所有的字串已经被转成了utf-8了,正在看是哪一步被转的。
ameyume 2010-08-21
  • 打赏
  • 举报
回复
用http://www.google.com.hk/ig/api?weather=Beijing查询,加big5转换也不行,还是乱码
yyy025025025 2010-08-21
  • 打赏
  • 举报
回复
这个编码应该是big5吧
ameyume 2010-08-21
  • 打赏
  • 举报
回复
public class WeatherReport extends Activity implements OnClickListener {
private static final String GOOGLE_API_URL = "http://www.google.com/ig/api?weather=";
private static final String NETWORK_ERROR = "Network error!";
private static final String TAG = "WEATHER_REPORT"; // Log tag
private Spinner spinner; // city list
private TextView tvWeatherInfo; // Display weather information

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

spinner = (Spinner) findViewById(R.id.spinner);

Button button = (Button) findViewById(R.id.goQuery);
button.setOnClickListener(this);
tvWeatherInfo = (TextView) findViewById(R.id.tvWeatherInfo);
}

@Override
public void onClick(View v) {
//获得用户输入的城市名称
String city = spinner.getSelectedItem().toString();

// 必须每次都重新创建一个新的task实例进行查询,否则将提示如下异常信息
// the task has already been executed (a task can be executed only once)
new GetWeatherTask().execute(city);

// Waiting message
tvWeatherInfo.setText(R.string.querying);
}

// Get weather information
public String getWeatherByCity(String city) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContent = new BasicHttpContext();
HttpGet httpGet = new HttpGet(GOOGLE_API_URL + city);

try {
HttpResponse response = httpClient.execute(httpGet, localContent);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG, "Failed. status code is not ok.");
httpGet.abort();
} else {
// Get weather information successful
HttpEntity httpEntity = response.getEntity();
return parseWeather(httpEntity.getContent());
}

} catch (Exception e) {
Log.e(TAG, "Failed to get weather information.", e);
e.printStackTrace();
} finally {
// disconnect network
httpClient.getConnectionManager().shutdown();
}

return NETWORK_ERROR;
}

// Parse InputStream to String
public static String parseWeather(InputStream is) throws IOException {
List<CurrentCondition> list = DomXMLReader.readXML(is);
CurrentCondition current = list.get(0);

return "天气现象:" + new String(current.getCondition().getBytes(), "GB2312") + "\n"
+ "华氏温度:" + current.getTemp_f() + "\n"
+ "摄氏温度:" + current.getTemp_c() + "℃\n"
+ current.getHumidity() + "\n"
// + current.getIconPath() + "\n"
+ current.getWind_condition();
}

// Get weather task extends AsyncTask
class GetWeatherTask extends AsyncTask<String, Integer, String> {

@Override
protected String doInBackground(String... params) {
String city = params[0];

// 调用Google天气API查询指定城市的当日天气情况
return getWeatherByCity(city);
}

protected void onPostExecute(String result) {
// 把doInBackground处理的结果即天气信息显示在TextView上
tvWeatherInfo.setText(result);
}
}
}


DomXMLReader.java
public class DomXMLReader {
public static List<CurrentCondition> readXML(InputStream inStream) {
List<CurrentCondition> currentList = new ArrayList<CurrentCondition>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(inStream);
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("current_conditions");//查找所有CurrentCondition节点
for (int i = 0; i < items.getLength(); i++) {
CurrentCondition currentCondition = new CurrentCondition();
//得到第一个CurrentCondition节点
Element currentNode = (Element) items.item(i);

//获取person节点下的所有子节点(标签之间的空白节点和name/age元素)
NodeList childsNodes = currentNode.getChildNodes();
for (int j = 0; j < childsNodes.getLength(); j++) {
Node node = (Node) childsNodes.item(j);
//判断是否为元素类型
if(node.getNodeType() == Node.ELEMENT_NODE){
Element childNode = (Element) node;
//判断是否condition元素
if ("condition".equals(childNode.getNodeName())) {
//获取condition节点的data属性值
currentCondition.setCondition(childNode.getAttribute("data"));
} else if ("temp_f".equals(childNode.getNodeName())) {
//获取temp_f节点的data属性值
currentCondition.setTemp_f(childNode.getAttribute("data"));
} else if ("temp_c".equals(childNode.getNodeName())) {
//获取temp_c节点的data属性值
currentCondition.setTemp_c(childNode.getAttribute("data"));
} else if ("humidity".equals(childNode.getNodeName())) {
//获取humidity节点的data属性值
currentCondition.setHumidity(childNode.getAttribute("data"));
} else if ("icon".equals(childNode.getNodeName())) {
//获取iconPath节点的data属性值
currentCondition.setIconPath(childNode.getAttribute("data"));
} else if ("wind_condition".equals(childNode.getNodeName())) {
//获取wind_condition节点的data属性值
currentCondition.setWind_condition(childNode.getAttribute("data"));
}
}
}
currentList.add(currentCondition);
}
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return currentList;
}
}


其中new String(current.getCondition().getBytes(), "GB2312")这样处理不起作用。
用http://www.google.com.hk/ig/api?weather=Beijing得到的就是乱码
各位看看该怎么改好?在哪个地方改呢?
haoyizsw 2010-08-21
  • 打赏
  • 举报
回复
根据传送过来的数据的编码格式,在本地提取信息的时候采用同样的编码就不会乱码了
ameyume 2010-08-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 l417584711 的回复:]
引用 4 楼 yyy025025025 的回复:
之前我用这个链接,出来的是中文:
http://www.google.cn/ig/api?weather=Beijing

现在得改成
http://www.google.com.hk/ig/api?weather=Beijing
O(∩_∩)O~
[/Quote]
这样出来时繁体,还是乱码,怎么设置编码呢?
aSysBang 2010-08-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yyy025025025 的回复:]
之前我用这个链接,出来的是中文:
http://www.google.cn/ig/api?weather=Beijing
[/Quote]
现在得改成
http://www.google.com.hk/ig/api?weather=Beijing
O(∩_∩)O~
加载更多回复(4)

80,353

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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