UrlConnection con = new UrlConnection("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=57713");
con.setEncode("utf-8");
try {
String result = con.connectUrl();
Document doc = DocumentHelper.parseText(result);
Element rootElt = doc.getRootElement(); // 获取根节点
Iterator iter = rootElt.elementIterator("string");
List<String> list = new ArrayList<String>();
while (iter.hasNext()) {
Element recordEle = (Element) iter.next();
list.add(recordEle.getStringValue());
}
HowjoyWeather wItem = new HowjoyWeather();
String temperature = list.get(5);
if (!StringHandler.isEmpty(temperature)) {
wItem.setTemperature(temperature);
wItem.setWeather(list.get(6).substring(6));
wItem.setWind(list.get(7));
wItem.setLogo(list.get(9));
this.getAndroidService().saveHowjoyWeather(wItem);
} else {
wItem.setTemperature("");
wItem.setWeather("");
wItem.setWind("");
wItem.setLogo("1.gif");
}
returnValue += gson.toJson(wItem);
} catch (IOException e) {
HowjoyWeather wItem = this.getAndroidService()
.findCurrentHowjoyWeather();
if (wItem == null) {
wItem = new HowjoyWeather();
wItem.setTemperature("");
wItem.setWeather("");
wItem.setWind("");
wItem.setLogo("1.gif");
}
returnValue += gson.toJson(wItem);
} catch (DocumentException e) {
HowjoyWeather wItem = this.getAndroidService()
.findCurrentHowjoyWeather();
if (wItem == null) {
wItem = new HowjoyWeather();
wItem.setTemperature("");
wItem.setWeather("");
wItem.setWind("");
wItem.setLogo("1.gif");
}
returnValue += gson.toJson(wItem);
}
获取天气
最新推荐文章于 2024-07-23 15:44:54 发布