大家好,我试图将美国国家航空航天局网站的RSS图片“当天的图像”解析到我的应用程序,但每次启动应用程序时都会崩溃并停止工作。我完全被同意了。 我的测试设备的API级别是16 谢谢inadvance
MainActivity代码:
public class NasaImageViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nasa_image_view);
xmlHandler handler = new xmlHandler();
handler.processFeed();
resetScreen(handler.getTitle(), handler.getDate(), handler.getDescription(), handler.getImage());
}
public void resetScreen (String titleSTR , String dateSTR , StringBuffer describe, Bitmap bm){
TextView title =(TextView) findViewById(R.id.title);
title.setText(titleSTR);
TextView date =(TextView) findViewById(R.id.pubDate);
date.setText(dateSTR);
TextView describtion =(TextView) findViewById(R.id.description);
describtion.setText(describe);
ImageView imgView = (ImageView) findViewById(R.id.image);
imgView.setImageBitmap(bm);
}
}
Sax解析器代码:
enter code here class xmlHandler extends DefaultHandler {
Boolean onItem = false, onEnclosure = false, onTitle = false, onPubDate = false , onDescription = false;
String title = null, date = null, url = "http://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss";
Bitmap image = null;
StringBuffer description = new StringBuffer();
public void processFeed(){
try{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(this);
InputStream is = new URL(url).openStream();
reader.parse(new InputSource(is));
}catch (Exception e){}
}
public Bitmap getBitmap (String url){
try{
HttpURLConnection connection =(HttpURLConnection) new URL(url).openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap image = BitmapFactory.decodeStream(input);
input.close();
return image;
}catch(IOException e){return null;}
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("item"))
onItem = true;
else if (onItem){
if (localName.equals("enclosure")) onEnclosure = true;
else onEnclosure = false;
if (localName.equals("title")) onPubDate = true;
else onPubDate = false;
if (localName.equals("pubDate")) onPubDate = true;
else onPubDate = false;
if (localName.equals("description")) onDescription = true;
else onDescription = false;
}
super.startElement(uri, localName, qName, attributes);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String chars =(String) new String(ch).subSequence(start, start+length);
if (onEnclosure && image == null) { image = getBitmap(chars); }
if (onTitle && title == null) { title = chars; }
if (onDescription) { description.append(chars); }
if (onPubDate && date == null) { date = chars; }
super.characters(ch, start, length);
}
public Bitmap getImage() { return image; }
public String getTitle() { return title; }
public StringBuffer getDescription() { return description; }
public String getDate() { return date; }
}