void updateTodayWeather(TodayWeather tw) { // 更新文字部分 titleCityTv.setText(tw.getCity() + "天气"); cityTv.setText(tw.getCity()); timeTv.setText(tw.getUpdatetime() + "发布"); humidityTv.setText("湿度" + tw.getShidu()); weekTv.setText(tw.getDate()); pmDataTv.setText(tw.getPm25()); pmQualityTv.setText(tw.getQuality()); temperatureTv.setText(tw.getLow() + "~" + tw.getHigh()); climateTv.setText(tw.getType()); windTv.setText("风力" + tw.getFengli()); // 更新天气图片 switch (tw.getType()) { case "暴雪": weatherImg.setImageResource(R.drawable.biz_plugin_weather_baoxue); break; case "暴雨": weatherImg.setImageResource(R.drawable.biz_plugin_weather_baoyu); break; case "大暴雨": weatherImg.setImageResource(R.drawable.biz_plugin_weather_dabaoyu); break; case "大雪": weatherImg.setImageResource(R.drawable.biz_plugin_weather_daxue); break; case "大雨": weatherImg.setImageResource(R.drawable.biz_plugin_weather_dayu); break; case "多云": weatherImg.setImageResource(R.drawable.biz_plugin_weather_duoyun); break; case "雷阵雨": weatherImg.setImageResource(R.drawable.biz_plugin_weather_leizhenyu); break; case "雷阵雨冰雹": weatherImg.setImageResource(R.drawable.biz_plugin_weather_leizhenyubingbao); break; case "晴": weatherImg.setImageResource(R.drawable.biz_plugin_weather_qing); break; case "沙尘暴": weatherImg.setImageResource(R.drawable.biz_plugin_weather_shachenbao); break; case "特大暴雨": weatherImg.setImageResource(R.drawable.biz_plugin_weather_tedabaoyu); break; case "小雪": weatherImg.setImageResource(R.drawable.biz_plugin_weather_xiaoxue); break; case "小雨": weatherImg.setImageResource(R.drawable.biz_plugin_weather_xiaoyu); break; case "阴": weatherImg.setImageResource(R.drawable.biz_plugin_weather_yin); break; case "雨夹雪": weatherImg.setImageResource(R.drawable.biz_plugin_weather_yujiaxue); break; case "阵雪": weatherImg.setImageResource(R.drawable.biz_plugin_weather_zhenxue); break; case "阵雨": weatherImg.setImageResource(R.drawable.biz_plugin_weather_zhenyu); break; case "中雪": weatherImg.setImageResource(R.drawable.biz_plugin_weather_zhongxue); break; case "中雨": weatherImg.setImageResource(R.drawable.biz_plugin_weather_zhongyu); break; default: weatherImg.setImageResource(R.drawable.biz_plugin_weather_wu); break; } // 更新PM2.5图片 if (tw.getPm25() != null) { int pmdata = Integer.parseInt(tw.getPm25()); if (pmdata >= 0 && pmdata <= 50) pmImg.setImageResource(R.drawable.biz_plugin_weather_0_50); else if (pmdata > 50 && pmdata <= 100) pmImg.setImageResource(R.drawable.biz_plugin_weather_51_100); else if (pmdata > 100 && pmdata <= 150) pmImg.setImageResource(R.drawable.biz_plugin_weather_101_150); else if (pmdata > 150 && pmdata <= 200) pmImg.setImageResource(R.drawable.biz_plugin_weather_151_200); else if (pmdata > 200 && pmdata <= 300) pmImg.setImageResource(R.drawable.biz_plugin_weather_201_300); else pmImg.setImageResource(R.drawable.biz_plugin_weather_greater_300); } Toast.makeText(MainActivity.this, "更新成功!", Toast.LENGTH_LONG).show(); UpdateBtn.setImageResource(R.drawable.title_update); }
// 解析函数 private TodayWeather parseXML(String xmldata) { TodayWeather todayweather = null; try { int fengxiangCount = 0; int fengliCount = 0; int dateCount = 0; int highCount = 0; int lowCount = 0; int typeCount = 0; XmlPullParserFactory fac = XmlPullParserFactory.newInstance(); XmlPullParser par = fac.newPullParser(); par.setInput(new StringReader(xmldata)); int eventType = par.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if (par.getName().equals("resp")) todayweather = new TodayWeather(); if (todayweather != null) { if (par.getName().equals("city")) { eventType = par.next(); todayweather.setCity(par.getText()); } else if (par.getName().equals("updatetime")) { eventType = par.next(); todayweather.setUpdatetime(par.getText()); } else if (par.getName().equals("shidu")) { eventType = par.next(); todayweather.setShidu(par.getText()); } else if (par.getName().equals("wendu")) { eventType = par.next(); todayweather.setWendu(par.getText()); } else if (par.getName().equals("pm25")) { eventType = par.next(); todayweather.setPm25(par.getText()); } else if (par.getName().equals("quality")) { eventType = par.next(); todayweather.setQuality(par.getText()); } else if (par.getName().equals("fengxiang") && fengxiangCount == 0) { eventType = par.next(); todayweather.setFengxiang(par.getText()); fengxiangCount++; } else if (par.getName().equals("fengli") && fengliCount == 0) { eventType = par.next(); todayweather.setFengli(par.getText()); fengliCount++; } else if (par.getName().equals("date") && dateCount == 0) { eventType = par.next(); todayweather.setDate(par.getText()); dateCount++; } else if (par.getName().equals("high") && highCount == 0) { eventType = par.next(); todayweather.setHigh(par.getText()); highCount++; } else if (par.getName().equals("low") && lowCount == 0) { eventType = par.next(); todayweather.setLow(par.getText()); lowCount++; } else if (par.getName().equals("type") && typeCount == 0) { eventType = par.next(); todayweather.setType(par.getText()); typeCount++; } } break; case XmlPullParser.END_TAG: break; } eventType = par.next(); } } catch (Exception e) { e.printStackTrace(); } return todayweather; }