Every time I use android, I always like to use the latest sdk (in fact, the system is deleted), so that I don't know when to download things in the main thread, not even a very small thing. This is also the specification of the program, because if the download is not completed for a period of time, the main thread remains in the suspended state, and the system will force it to close. However, we often need to update the UI after downloading data. This causes two to be not in one thread. Below are some codes to solve this problem.
Package com.yx.mp3 player;
Import java. io. StringReader;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. List;
Import javax. xml. parsers. ParserConfigurationException;
Import javax. xml. parsers. SAXParserFactory;
Import org. xml. sax. InputSource;
Import org. xml. sax. SAXException;
Import org. xml. sax. XMLReader;
Import com. yx. download. HttpDownload;
Import com. yx. model. Mp3Info;
Import com. yx. xml. Mp3ListContentHandler;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. HandlerThread;
Import android. app. Activity;
Import android. app. ListActivity;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. widget. SimpleAdapter;
Public class Mp3ListActivity extends ListActivity {
Private static final int UPDATE = 1;
Private static final int ABOUT = 1;
Private HandlerThread handlerThread;
Private Handler handler;
Private List Mp3Infos = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_mp3_list );
Handler = new Handler ();
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
Getmenuinflater().inflate(r.menu1_list, menu );
Menu. add (0, UPDATE, 1, "UPDATE ");
Menu. add (0, ABOUT, 2, "ABOUT ");
Return true;
}
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
If (1 = item. getItemId ()){
// Click the update button.
New Thread (new Runnable (){
@ Override
Public void run (){
// Download
String xmlStr;
XmlStr = downLoadXML ("http: // 192.168.0.103: 8080/mp3/resouces. xml ");
Mp3Infos = parse (xmlStr );
Handler. post (new Runnable (){
@ Override
Public void run (){
// Update the UI
List > List = new ArrayList > ();
For (Iterator iterator = mp3Infos. iterator (); iterator. hasNext ();){
Mp3Info mp3Info = (Mp3Info) iterator. next ();
HashMap Map = new HashMap ();
Map. put ("mp3_name", mp3Info. getMp3Name ());
Map. put ("mp3_size", mp3Info. getMp3Size ());
List. add (map );
}
SimpleAdapter simpleAdapter = new SimpleAdapter (Mp3ListActivity. this, list,
R.layout.mp3 info_item,
New String [] {"mp3_name", "mp3_size "},
New int[{r.id.mp3_name,r.id1_size });
SetListAdapter (simpleAdapter );
System. out. println ("=================== ");
}});
}
}). Start ();
} Else if (ABOUT = item. getItemId ()){
// The user clicks the related button
}
Return super. onOptionsItemSelected (item );
}
Public String downLoadXML (String urlstr ){
HttpDownload httpDownloader = new HttpDownload ();
String result = httpDownloader. downLoad (urlstr );
System. out. println ("XML --->" + result );
Return result;
}
Private List Parse (String xmlStr ){
List Infos = new ArrayList ();
SAXParserFactory saxParserFactory = SAXParserFactory. newInstance ();
Try {
XMLReader xmlReader = saxParserFactory. newSAXParser (). getXMLReader ();
Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler (infos );
XmlReader. setContentHandler (mp3ListContentHandler );
XmlReader. parse (new InputSource (new StringReader (xmlStr )));
} Catch (Exception e ){
E. printStackTrace ();
}
Return infos;
}
}