一、项目设计
二、歌曲列表简介
1.利用java.net.HttpURLConnection以流的形式下载xml文件为String
2.自定义ContentHandler--》Mp3ListContentHandler
3.利用自定义的ContentHandler和javax.xml.parsers.SAXParserFactory生成org.xml.sax.XMLReader,用来处理下载的xml字符串,处理结果以List<Mp3Info>返回
4.以返回的List<Mp3Info>和布局文件创建android.widget.SimpleAdapter
5.最后在activity中调用setListAdapter(adapter),完成更新列表操作
二、代码
1.xml
(1)main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill_parent" 4 android:layout_height="fill_parent"> 5 <LinearLayout android:id="@+id/listLinearLayout" 6 android:layout_width="fill_parent" android:layout_height="wrap_content" 7 android:orientation="vertical"> 8 <ListView android:id="@id/android:list" android:layout_width="fill_parent" 9 android:layout_height="wrap_content" android:drawSelectorOnTop="false" 10 android:scrollbars="vertical" /> 11 </LinearLayout> 12 </LinearLayout>
(2)AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="tony.mp3player" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="21" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name=".Mp3ListActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 <uses-permission android:name="android.permission.INTERNET"/> 27 </manifest>
3.mp3info_item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="horizontal" 6 android:paddingLeft="10dip" 7 android:paddingRight="10dip" 8 android:paddingTop="1dip" 9 android:paddingBottom="1dip" 10 > 11 <TextView android:id="@+id/mp3Name" 12 android:layout_height="30dip" 13 android:layout_width="180dip" 14 android:textSize="10pt"/> 15 <TextView android:id="@+id/mp3Size" 16 android:layout_height="30dip" 17 android:layout_width="180dip" 18 android:textSize="10pt"/> 19 </LinearLayout>
2.java
(1)Mp3ListActivity.java
1 package tony.mp3player; 2 3 import java.io.IOException; 4 import java.io.StringReader; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.Iterator; 8 import java.util.List; 9 import java.util.Map; 10 11 import javax.xml.parsers.ParserConfigurationException; 12 import javax.xml.parsers.SAXParserFactory; 13 14 import org.xml.sax.InputSource; 15 import org.xml.sax.SAXException; 16 import org.xml.sax.XMLReader; 17 18 import tony.download.HttpDownloader; 19 import tony.model.Mp3Info; 20 import tony.xml.Mp3ListContentHandler; 21 import android.annotation.SuppressLint; 22 import android.app.ListActivity; 23 import android.os.Bundle; 24 import android.os.StrictMode; 25 import android.view.Menu; 26 import android.view.MenuItem; 27 import android.widget.SimpleAdapter; 28 29 @SuppressLint("NewApi") 30 public class Mp3ListActivity extends ListActivity { 31 private static final int UPDATE = 1; 32 private static final int ABOUT = 1; 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.main); 37 if (android.os.Build.VERSION.SDK_INT > 9) { 38 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 39 StrictMode.setThreadPolicy(policy); 40 } 41 updateListView(); 42 } 43 44 /** 45 * 在用点击MENU按钮之后,会调用该方法,我们可以在这个方法当中加入自己的按钮控件 46 */ 47 @Override 48 public boolean onCreateOptionsMenu(Menu menu) { 49 menu.add(0, UPDATE, UPDATE, R.string.mp3list_update); 50 menu.add(0, ABOUT, ABOUT, R.string.mp3list_about); 51 return super.onCreateOptionsMenu(menu); 52 } 53 54 /** 55 * 当菜单选项被选中时调用 56 */ 57 @Override 58 public boolean onOptionsItemSelected(MenuItem item) { 59 System.out.println("onOptionsItemSelected--->ItemId="+item.getItemId()); 60 switch (item.getItemId()) { 61 case UPDATE: 62 updateListView(); 63 break; 64 case 2: 65 break; 66 default: 67 break; 68 } 69 return super.onOptionsItemSelected(item); 70 } 71 72 private void updateListView() { 73 String xml = downloadXML("http://192.168.1.104:8080/mp3/resources.xml"); 74 List<Mp3Info> infos = parse(xml); 75 SimpleAdapter adapter = buidSimpleAdapter(infos); 76 setListAdapter(adapter); 77 } 78 79 private SimpleAdapter buidSimpleAdapter(List<Mp3Info> infos) { 80 // 生成一个List对象,并按照SimpleAdapter的标准,将mp3Infos当中的数据添加到List当中去 81 List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 82 HashMap<String,String> map = null; 83 Mp3Info info = null; 84 for(Iterator it = infos.iterator() ; it.hasNext() ;) { 85 info = (Mp3Info) it.next(); 86 map = new HashMap<String,String>(); 87 map.put("mp3Name", info.getMp3Name()); 88 map.put("mp3Size", info.getMp3Size()); 89 list.add(map); 90 } 91 SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.mp3info_item, 92 new String[]{"mp3Name", "mp3Size"}, new int[]{R.id.mp3Name, R.id.mp3Size}); 93 return adapter; 94 } 95 96 private String downloadXML(String urlStr) { 97 HttpDownloader httpDownloader = new HttpDownloader(); 98 String result = httpDownloader.download(urlStr); 99 return result; 100 } 101 102 private List<Mp3Info> parse(String xml) { 103 SAXParserFactory spf = SAXParserFactory.newInstance(); 104 List<Mp3Info> infos = new ArrayList<Mp3Info>(); 105 try { 106 XMLReader reader = spf.newSAXParser().getXMLReader(); 107 Mp3ListContentHandler handler = new Mp3ListContentHandler(infos); 108 reader.setContentHandler(handler); 109 reader.parse(new InputSource(new StringReader(xml))); 110 for(Iterator it = infos.iterator();it.hasNext();) { 111 Mp3Info info = (Mp3Info) it.next(); 112 System.out.println(info); 113 } 114 } catch (SAXException e) { 115 e.printStackTrace(); 116 } catch (ParserConfigurationException e) { 117 e.printStackTrace(); 118 } catch (IOException e) { 119 e.printStackTrace(); 120 } 121 return infos; 122 } 123 }
(2)Mp3ListContentHandler.java
1 package tony.xml; 2 3 import java.util.List; 4 5 import org.xml.sax.Attributes; 6 import org.xml.sax.SAXException; 7 import org.xml.sax.helpers.DefaultHandler; 8 9 import tony.model.Mp3Info; 10 11 public class Mp3ListContentHandler extends DefaultHandler { 12 13 private List<Mp3Info> infos = null; 14 private Mp3Info mp3Info = null; 15 private String tagName = null; 16 17 public Mp3ListContentHandler(List<Mp3Info> infos) { 18 super(); 19 this.infos = infos; 20 } 21 22 @Override 23 public void startDocument() throws SAXException { 24 super.startDocument(); 25 } 26 27 @Override 28 public void endDocument() throws SAXException { 29 super.endDocument(); 30 } 31 32 @Override 33 public void startElement(String uri, String localName, String qName, 34 Attributes attributes) throws SAXException { 35 this.tagName = localName; 36 if(tagName.equals("resource")) { 37 mp3Info = new Mp3Info(); 38 } 39 } 40 41 @Override 42 public void endElement(String uri, String localName, String qName) 43 throws SAXException { 44 if(qName.equals("resource")) { 45 infos.add(mp3Info); 46 } 47 tagName = ""; 48 } 49 50 @Override 51 public void characters(char[] ch, int start, int length) 52 throws SAXException { 53 String temp = new String(ch, start, length); 54 if(tagName.equals("id")) 55 mp3Info.setId(temp); 56 else if(tagName.equals("mp3.name")) 57 mp3Info.setMp3Name(temp); 58 else if(tagName.equals("mp3.size")) 59 mp3Info.setMp3Size(temp); 60 else if(tagName.equals("lrc.name")) 61 mp3Info.setLrcName(temp); 62 else if(tagName.equals("lrc.size")) 63 mp3Info.setLrcSize(temp); 64 } 65 }
(3)HttpDownloader.java
1 package tony.download; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 8 public class HttpDownloader { 9 10 /** 11 * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容 12 * 1.创建一个URL对象 13 * 2.通过URL对象,创建一个HttpURLConnection对象 14 * 3.得到InputStram 15 * 4.从InputStream当中读取数据 16 * @param urlStr 17 * @return 18 */ 19 public String download(String urlStr) { 20 StringBuffer sb = new StringBuffer(); 21 String line = null; 22 BufferedReader buffer = null; 23 try { 24 // 创建一个URL对象 25 URL url = new URL(urlStr); 26 // 创建一个Http连接 27 HttpURLConnection urlConn = (HttpURLConnection) url 28 .openConnection(); 29 // 使用IO流读取数据 30 buffer = new BufferedReader(new InputStreamReader(urlConn 31 .getInputStream())); 32 while ((line = buffer.readLine()) != null) { 33 sb.append(line); 34 } 35 } catch (Exception e) { 36 e.printStackTrace(); 37 } finally { 38 try { 39 buffer.close(); 40 } catch (Exception e) { 41 e.printStackTrace(); 42 } 43 } 44 return sb.toString(); 45 } 46 }
(4)
1 package tony.model; 2 3 public class Mp3Info { 4 5 private String id,mp3Name,mp3Size,lrcName,lrcSize; 6 7 public Mp3Info() { 8 super(); 9 } 10 11 public Mp3Info(String id, String mp3Name, String mp3Size, String lrcName, 12 String lrcSize) { 13 super(); 14 this.id = id; 15 this.mp3Name = mp3Name; 16 this.mp3Size = mp3Size; 17 this.lrcName = lrcName; 18 this.lrcSize = lrcSize; 19 } 20 21 @Override 22 public String toString() { 23 return "Mp3Info [id=" + id + ", mp3Name=" + mp3Name + ", mp3Size=" 24 + mp3Size + ", lrcName=" + lrcName + ", lrcSize=" + lrcSize 25 + "]"; 26 } 27 28 public String getId() { 29 return id; 30 } 31 32 public void setId(String id) { 33 this.id = id; 34 } 35 36 public String getMp3Name() { 37 return mp3Name; 38 } 39 40 public void setMp3Name(String mp3Name) { 41 this.mp3Name = mp3Name; 42 } 43 44 public String getMp3Size() { 45 return mp3Size; 46 } 47 48 public void setMp3Size(String mp3Size) { 49 this.mp3Size = mp3Size; 50 } 51 52 public String getLrcName() { 53 return lrcName; 54 } 55 56 public void setLrcName(String lrcName) { 57 this.lrcName = lrcName; 58 } 59 60 public String getLrcSize() { 61 return lrcSize; 62 } 63 64 public void setLrcSize(String lrcSize) { 65 this.lrcSize = lrcSize; 66 } 67 68 69 }
四、运行结果