Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created June 11, 2010 06:25
Show Gist options
  • Select an option

  • Save shaobin0604/434124 to your computer and use it in GitHub Desktop.

Select an option

Save shaobin0604/434124 to your computer and use it in GitHub Desktop.
private void parseWeather() {
mForecasts = new ArrayList<Map<String,String>>();
mCondition = new HashMap<String, String>();
InputStream is = null;
try {
// is = getLocalWeatherSource();
is = getWebWeatherSource();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(is, "UTF-8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if (eventType == XmlPullParser.END_DOCUMENT) {
System.out.println("End document");
} else if (eventType == XmlPullParser.START_TAG) {
String tagName = xpp.getName();
System.out.println("Start tag " + tagName);
if ("current".equals(tagName)) {
mCondition.put("temperature", xpp.getAttributeValue(null, "temperature"));
mCondition.put("skycode", xpp.getAttributeValue(null, "skycode"));
mCondition.put("skytext", xpp.getAttributeValue(null, "skytext"));
mCondition.put("date", xpp.getAttributeValue(null, "date"));
mCondition.put("observationtime", xpp.getAttributeValue(null, "observationtime"));
mCondition.put("observationpoint", xpp.getAttributeValue(null, "observationpoint"));
mCondition.put("feelslike", xpp.getAttributeValue(null, "feelslike"));
mCondition.put("humidity", xpp.getAttributeValue(null, "humidity"));
mCondition.put("windspeed", xpp.getAttributeValue(null, "windspeed"));
} else if ("forecast".equals(tagName)) {
Map<String, String> forecast = new HashMap<String, String>();
forecast.put("low", xpp.getAttributeValue(null, "low"));
forecast.put("high", xpp.getAttributeValue(null, "high"));
forecast.put("skycodeday", xpp.getAttributeValue(null, "skycodeday"));
forecast.put("skytextday", xpp.getAttributeValue(null, "skytextday"));
forecast.put("date", xpp.getAttributeValue(null, "date"));
forecast.put("precip", xpp.getAttributeValue(null, "precip"));
mForecasts.add(forecast);
}
} else if (eventType == XmlPullParser.END_TAG) {
System.out.println("End tag " + xpp.getName());
} else if (eventType == XmlPullParser.TEXT) {
System.out.println("Text " + xpp.getText());
}
eventType = xpp.next();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
StringBuilder str = new StringBuilder();
str.append("------- condition -------\n");
str.append(mCondition.toString());
str.append('\n');
str.append("------- forecasts -------\n");
for (Map<String, String> forecast : mForecasts) {
str.append(forecast.get("date"));
str.append('\n');
str.append(forecast.toString());
str.append("\n\n");
}
mProgressBar.setVisibility(View.GONE);
mTextView.setVisibility(View.VISIBLE);
mTextView.setText(str);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null)
try {
is.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment