问题:xmlReader.parse(new InputSource(new StringReader(xmlStr))); 这行代码为什么执行不了?
理由:System.out.println("xml------------");放在上行代码前能打印出来,放到之后就打印不出来,并且log里面提醒的也是这行代码!
全部代码如下:
package your.napo;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.SAXParserFactory;
import napo.download.HttpDownload;
import napo.model.Mp3Info;
import napo.xml.Mp3ListContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Mp3player extends ListActivity {
/** Called when the activity is first created. */
private static final int UPDATE = 1;
private static final 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.mp3list_update);
menu.add(0, ABOUT, 2, R.string.mp3list_about);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == UPDATE) {
String xml = downloadXML("http://192.168.1.2:8080/mp3/resources.xml");
parse(xml);
}
else if(item.getItemId() == ABOUT) {
}
return super.onOptionsItemSelected(item);
}
private String downloadXML(String urlStr) {
HttpDownload httpDownload = new HttpDownload();
String result = httpDownload.download(urlStr);
return result;
}
private List<Mp3Info> parse(String xmlStr) {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
List<Mp3Info> infos = new ArrayList<Mp3Info>();
try {
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(infos);
xmlReader.setContentHandler(mp3ListContentHandler);
System.out.println("xml------------");
xmlReader.parse(new InputSource(new StringReader(xmlStr)));
for (Iterator iterator = infos.iterator(); iterator.hasNext();) {
Mp3Info mp3Info = (Mp3Info) iterator.next();
System.out.println(mp3Info);
}
} catch(Exception e) {
e.printStackTrace();
}
return infos;
}
}
Log如下:
05-23 06:43:08.289: W/System.err(1102): org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 377: mismatched tag
05-23 06:43:08.289: W/System.err(1102): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:507)
05-23 06:43:08.298: W/System.err(1102): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:492)
05-23 06:43:08.298: W/System.err(1102): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:308)
05-23 06:43:08.298: W/System.err(1102): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:264)
05-23 06:43:08.298: W/System.err(1102): at your.napo.Mp3player.parse(Mp3player.java:68)
05-23 06:43:08.298: W/System.err(1102): at your.napo.Mp3player.onOptionsItemSelected(Mp3player.java:45)
05-23 06:43:08.298: W/System.err(1102): at android.app.Activity.onMenuItemSelected(Activity.java:2195)
05-23 06:43:08.298: W/System.err(1102): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)