我是android新手,我想开发一个示例应用程序来显示我的android应用程序中的天气,我正在使用URL“http://weather.yahooapis.com/forecastrss?w=2295425&u=c””,在我的应用程序中所有字段的输出都返回null。我无法解析url.please请帮助我。这是我正在使用的代码:
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;
public class YahooWeatherMainActivity extends Activity {
String url = "http://weather.yahooapis.com/forecastrss?w=2295425&u=c”";
SetterGetter setterGetter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yahoo_weather_main);
abc();
}
public void abc() {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
TextView today_date;
TextView city;
TextView region;
TextView country;
TextView temp;
TextView humidity;
TextView wind;
TextView sunnise;
TextView sunset;
TextView date[];
TextView day[];
TextView low[];
TextView high[];
TextView text[];
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL sourceUrl = new URL(url);
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
setterGetter = MyXMLHandler.setterGetter;
today_date = new TextView(this);
today_date.setText("Today_Date = " + setterGetter.getToday_date());
city = new TextView(this);
city.setText("City = " + setterGetter.getCity());
region = new TextView(this);
region.setText("region = " + setterGetter.getRegion());
country = new TextView(this);
country.setText("Date = " + setterGetter.getCountry());
temp = new TextView(this);
temp.setText("temp = " + setterGetter.getTemp());
humidity = new TextView(this);
humidity.setText("humidity = " + setterGetter.getHumidity());
wind = new TextView(this);
wind.setText("wind = " + setterGetter.getWind());
sunnise = new TextView(this);
sunnise.setText("sunnise = " + setterGetter.getSunnise());
sunset = new TextView(this);
sunset.setText("Sunset = " + setterGetter.getSunset());
date = new TextView[setterGetter.getDate().size()];
day = new TextView[setterGetter.getDate().size()];
low = new TextView[setterGetter.getDate().size()];
high = new TextView[setterGetter.getDate().size()];
text = new TextView[setterGetter.getDate().size()];
layout.addView(today_date);
layout.addView(city);
layout.addView(region);
layout.addView(country);
layout.addView(temp);
layout.addView(humidity);
layout.addView(wind);
layout.addView(sunnise);
layout.addView(sunset);
for (int i = 0; i < setterGetter.getDate().size(); i++) {
date[i] = new TextView(this);
date[i].setText("Date = " + setterGetter.getDate().get(i));
day[i] = new TextView(this);
day[i].setText("Day = " + setterGetter.getDay().get(i));
low[i] = new TextView(this);
low[i].setText("low = " + setterGetter.getLow().get(i));
high[i] = new TextView(this);
high[i].setText("high = " + setterGetter.getHigh().get(i));
text[i] = new TextView(this);
text[i].setText("Condition = " + setterGetter.getText().get(i));
layout.addView(date[i]);
layout.addView(day[i]);
layout.addView(low[i]);
layout.addView(high[i]);
layout.addView(text[i]);
}
setContentView(layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.yahoo_weather_main, menu);
return true;
}
}
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyXMLHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue = null;
public static SetterGetter setterGetter = null;
public static SetterGetter getList() {
return setterGetter;
}
public static void setSitesList(SetterGetter sitesList) {
MyXMLHandler.setterGetter = setterGetter;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
if (localName.equalsIgnoreCase("lastBuildDate"))
setterGetter.setToday_date(currentValue);
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("rss")) {
setterGetter = new SetterGetter();
} else if (localName.equals("channel")) {
String date = attributes.getValue("date");
setterGetter.setDate(date);
String day = attributes.getValue("day");
setterGetter.setDay(day);
String low = attributes.getValue("low");
setterGetter.setLow(low);
String high = attributes.getValue("high");
setterGetter.setHigh(high);
String text = attributes.getValue("text");
setterGetter.setText(text);
} else if (localName.equals("yweather:location")) {
String city = attributes.getValue("city");
setterGetter.setCity(city);
String region = attributes.getValue("region");
setterGetter.setRegion(region);
String country = attributes.getValue("country");
setterGetter.setCountry(country);
}
else if (localName.equals("yweather:wind")) {
String chill = attributes.getValue("chill");
setterGetter.setTemp(chill);
String speed = attributes.getValue("speed");
setterGetter.setWind(speed);
} else if (localName.equals("yweather:atmosphere")) {
String humidity = attributes.getValue("humidity");
setterGetter.setHumidity(humidity);
} else if (localName.equals("yweather:astronomy")) {
String sunrise = attributes.getValue("sunrise");
setterGetter.setSunnise(sunrise);
String sunset = attributes.getValue("sunset");
setterGetter.setSunset(sunset);
}
}
}