public class Mp3Player extends ListActivity {
/** Called when the activity is first created. */
public final static int UPDATE = 1;
public final static int ABOUT = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, UPDATE, 1, R.string.update);
menu.add(0, ABOUT, 2, R.string.about);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId() == UPDATE) {
String xml = downloadXml("http://192.168.254.1:8080/mp3/resouces.xml");
[size=large][color=red](1)System.out.println("xml--------"+xml);[/color][/size] parse(xml);
}
else if(item.getItemId() == ABOUT) {
}
return super.onOptionsItemSelected(item);
}
public String downloadXml(String urlStr) {
HttpDownloader download = new HttpDownloader();
String result = download.download(urlStr);
return result;
}
public List<Mp3Info> parse(String xmlStr) {
//创建sax解析器工厂
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
StringReader strR = new StringReader(xmlStr);
InputSource input = new InputSource(strR);
System.out.println("xml--------"+xmlStr);
try {
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
List<Mp3Info> info = new ArrayList<Mp3Info>();
Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(info);
xmlReader.setContentHandler(mp3ListContentHandler);
xmlReader.parse(input);
[color=red](2)System.out.println("info size-----------"+info.size());
(3)for(Iterator iterator = info.iterator(); iterator.hasNext();) {
Mp3Info mp3Info = (Mp3Info)iterator.next();
System.out.println("mp3Info-------"+mp3Info);
}[/color] } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
上面是activity类
(1)处可以输出完整的xml信息
(2)(3)处均不能被执行
public class Mp3ListContentHandler extends DefaultHandler {
private List<Mp3Info> infos = null;
private Mp3Info mp3Info = null;
private String targetName = null;
public Mp3ListContentHandler(List<Mp3Info> infos) {
super();
this.infos = infos;
}
public List<Mp3Info> getInfos() {
return infos;
}
public void setInfos(List<Mp3Info> infos) {
this.infos = infos;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String temp = new String(ch, start, length);
if(targetName.equals("id")) {
mp3Info.setId(temp);
}
else if(targetName.equals("mp3.name")) {
mp3Info.setMp3Name(temp);
}
else if(targetName.equals("mp3.size")) {
mp3Info.setMp3Size(temp);
}
else if(targetName.equals("lrc.name")) {
mp3Info.setLrcName(temp);
}
else if(targetName.equals("lrc.size")) {
mp3Info.setLrcSize(temp);
}
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
[size=large][color=red](1)public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("endDocument methoed is start-----");
}[/color][/size] @Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if(qName.equals("resource")) {
infos.add(mp3Info);
[size=large][color=red](2)System.out.println("infos is --------"+infos);[/color][/size] }
this.targetName = "";
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
this.targetName = localName;
if(targetName.equals("resource")) {
mp3Info = new Mp3Info();
}
}
}
这是处理xml文件的Handler
(1)处方法不能被执行
(2)处可以输出正常信息
小弟被这个问题卡了好几天了,恳求大神们指点指点!