mp3播放器(一)(从服务器端下载xml文件)
Mp3ListActivity.java
1 package com.example.mp3player00; 2 3 import java.io.IOException; 4 import java.io.StringReader; 5 import java.util.ArrayList; 6 import java.util.Iterator; 7 import java.util.List; 8 9 import javax.xml.parsers.ParserConfigurationException; 10 import javax.xml.parsers.SAXParserFactory; 11 12 import org.xml.sax.InputSource; 13 import org.xml.sax.SAXException; 14 import org.xml.sax.XMLReader; 15 16 import xml.Mp3ListContentHandler; 17 18 import model.Mp3Info; 19 20 import com.example.download.HttpDownloader; 21 22 import android.os.Bundle; 23 import android.app.Activity; 24 import android.app.ListActivity; 25 import android.view.Menu; 26 import android.view.MenuItem; 27 28 public class Mp3ListActivity extends ListActivity { 29 private static final int upadate=1; 30 private static final int about=2; 31 //以上定义常量,看到值的名字就知道按钮的名字 32 //且为后面修改提供方便 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.activity_mp3_list); 37 } 38 39 @Override 40 public boolean onCreateOptionsMenu(Menu menu) { 41 // Inflate the menu; this adds items to the action bar if it is present. 42 getMenuInflater().inflate(R.menu.activity_mp3_list, menu); 43 menu.add(0, 1, 1,R.string.mp3list_update); 44 menu.add(0, 2, 2, R.string.mp3list_about); 45 return true; 46 } 47 48 @Override 49 public boolean onOptionsItemSelected(MenuItem item) { 50 // TODO Auto-generated method stub 51 //System.out.println("--->"+item.getItemId()); 52 if(item.getItemId()==upadate) 53 { 54 //用户点击了更新按钮 55 56 String xml=downloadXML("http://192.168.1.100:8080/mp3/resources.xml"); 57 //System.out.println("----->"+xml); 58 try { 59 parse(xml); 60 } catch (IOException e) { 61 // TODO Auto-generated catch block 62 e.printStackTrace(); 63 } 64 } 65 else if(item.getItemId()==about) 66 { 67 //用户点击了关于按钮 68 } 69 return super.onOptionsItemSelected(item); 70 } 71 //写一个单独的下载函数,更加清晰 72 private String downloadXML(String urlStr) 73 { 74 75 HttpDownloader httpdownloader=new HttpDownloader(); 76 String result=httpdownloader.download(urlStr); 77 return result; 78 } 79 //在这个函数中解析xml 80 //首先生成xml解析器 81 private List<Mp3Info> parse(String xmlStr) throws IOException 82 { 83 SAXParserFactory saxParserFactory=SAXParserFactory.newInstance(); 84 try { 85 XMLReader xmlReader=saxParserFactory.newSAXParser().getXMLReader(); 86 List<Mp3Info> info=new ArrayList<Mp3Info>(); 87 Mp3ListContentHandler mp3ListContentHandler=new Mp3ListContentHandler(info); 88 xmlReader.setContentHandler(mp3ListContentHandler); 89 xmlReader.parse(new InputSource(new StringReader(xmlStr))); 90 //解析完数据都天道info里面,然后用for循环迭代出来 91 for (Iterator iterator = info.iterator(); iterator.hasNext();) { 92 Mp3Info mp3Info = (Mp3Info) iterator.next(); 93 System.out.println(mp3Info); 94 } 95 96 } catch (SAXException e) { 97 // TODO Auto-generated catch block 98 e.printStackTrace(); 99 } catch (ParserConfigurationException e) { 100 // TODO Auto-generated catch block 101 e.printStackTrace(); 102 } 103 104 return null; 105 } 106 107 }
HttpDownloader.java
1 package com.example.download; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 import java.net.MalformedURLException; 8 import java.net.URL; 9 10 public class HttpDownloader { 11 12 /** 13 * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容 14 * 1.创建一个URL对象 15 * 2.通过URL对象,创建一个HttpURLConnection对象 16 * 3.得到InputStram 17 * 4.从InputStream当中读取数据 18 * @param urlStr 19 * @return 20 */ 21 public String download(String urlStr) { 22 StringBuffer sb = new StringBuffer(); 23 String line = null; 24 BufferedReader buffer = null; 25 try { 26 // 创建一个URL对象 27 URL url = new URL(urlStr); 28 // 创建一个Http连接 29 HttpURLConnection urlConn = (HttpURLConnection) url 30 .openConnection(); 31 // 使用IO流读取数据 32 buffer = new BufferedReader(new InputStreamReader(urlConn 33 .getInputStream())); 34 while ((line = buffer.readLine()) != null) { 35 sb.append(line); 36 } 37 } catch (Exception e) { 38 e.printStackTrace(); 39 } finally { 40 try { 41 buffer.close(); 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } 45 } 46 return sb.toString(); 47 } 48 }
Mp3Info.java
1 package model; 2 /** 3 * 把每一个<rescource>看成一个对象 mp3info对象 建一个实体类,代表现实中一个实体 4 * Mp3info实体类 5 * @author kyx 6 */ 7 public class Mp3Info { 8 private String id; 9 private String mp3Name; 10 private String mp3Size; 11 private String IrcName; 12 private String IrcSize; 13 public String getId() { 14 return id; 15 } 16 public void setId(String id) { 17 this.id = id; 18 } 19 public String getMp3Name() { 20 return mp3Name; 21 } 22 public void setMp3Name(String mp3Name) { 23 this.mp3Name = mp3Name; 24 } 25 public String getMp3Size() { 26 return mp3Size; 27 } 28 public void setMp3Size(String mp3Size) { 29 this.mp3Size = mp3Size; 30 } 31 public String getIrcName() { 32 return IrcName; 33 } 34 public void setIrcName(String ircName) { 35 IrcName = ircName; 36 } 37 public String getIrcSize() { 38 return IrcSize; 39 } 40 public void setIrcSize(String ircSize) { 41 IrcSize = ircSize; 42 } 43 //还要生成一些构造函数,构造函数起码要有两个,一个是带参数的,一个是不带参数的 44 public Mp3Info(String id, String mp3Name, String mp3Size, String ircName, 45 String ircSize) { 46 super(); 47 this.id = id; 48 this.mp3Name = mp3Name; 49 this.mp3Size = mp3Size; 50 IrcName = ircName; 51 IrcSize = ircSize; 52 } 53 //不带参数的的构造函数 54 public Mp3Info() { 55 super(); 56 } 57 @Override 58 public String toString() { 59 return "Mp3Info [id=" + id + ", mp3Name=" + mp3Name + ", mp3Size=" 60 + mp3Size + ", IrcName=" + IrcName + ", IrcSize=" + IrcSize + "]"; 61 } 62 //toString方便测试鉴定 63 }
Mp3ContentHandler.java
1 package xml; 2 3 import java.util.List; 4 5 import model.Mp3Info; 6 7 import org.xml.sax.Attributes; 8 import org.xml.sax.SAXException; 9 import org.xml.sax.helpers.DefaultHandler; 10 //解析 11 //内容处理器 ContentHandler 接口是解析xm的主体 12 //handler使用的是adapter的设计模式 13 //Adapter是连接后端数据和前端显示的适配器接口, 14 //是数据和UI(View)之间一个重要的纽带。在常见的View(ListView,GridView)等地方都需要用到Adapter 15 public class Mp3ListContentHandler extends DefaultHandler { 16 private List<Mp3Info> info=null;//这边每次解析resource标签都会放入一个Mp3Info对象当中 ,一个标签就是一resource,再把每个对象放入List 17 //Mp3ListActivity在调用此类的时候会生成List并且传进来 18 private Mp3Info mp3info=null;//mp3info里面就是存放resource的值 19 private String tagname; 20 21 //添加get和set方便访问 22 public List<Mp3Info> getInfo() { 23 return info; 24 } 25 26 public void setInfo(List<Mp3Info> info) { 27 this.info = info; 28 } 29 30 //Mp3ListActivity在调用此类的时候会生成List并且传进来 31 public Mp3ListContentHandler(List<Mp3Info> info) { 32 super(); 33 this.info = info; 34 } 35 36 @Override 37 //遇到id这个标签,将值赋给Mp3Info的id属性 38 public void characters(char[] ch, int start, int length) 39 throws SAXException { 40 // TODO Auto-generated method stub 41 super.characters(ch, start, length); 42 String temp=new String(ch,start,length); 43 if(tagname.equals("id")) 44 { 45 mp3info.setId(temp);//如果标签是“id”,就把值赋予mp3info对象中的id变量中 46 } 47 else if(tagname.equals("mp3.name")) 48 { 49 mp3info.setMp3Name(temp); 50 } 51 else if(tagname.equals("mp3.size")) 52 { 53 mp3info.setMp3Size(temp); 54 } 55 else if(tagname.equals("Irc.name")) 56 { 57 mp3info.setIrcName(temp); 58 } 59 else if(tagname.equals("Irc.size")) 60 { 61 mp3info.setIrcSize(temp); 62 } 63 64 } 65 66 @Override 67 public void endDocument() throws SAXException { 68 // TODO Auto-generated method stub 69 super.endDocument(); 70 } 71 72 @Override 73 //标签解析结束 74 public void endElement(String uri, String localName, String qName) 75 throws SAXException { 76 // TODO Auto-generated method stub 77 super.endElement(uri, localName, qName); 78 if(qName.equals("resource"))//这里用qName而不是tagname 79 { 80 info.add(mp3info);//如果遇到resource结尾,那么就把这个对象放入到List中 81 } 82 tagname=""; 83 } 84 85 @Override 86 public void startDocument() throws SAXException { 87 // TODO Auto-generated method stub 88 super.startDocument(); 89 } 90 91 @Override 92 //开始解析标签 93 public void startElement(String uri, String localName, String qName, 94 Attributes attributes) throws SAXException { 95 // TODO Auto-generated method stub 96 super.startElement(uri, localName, qName, attributes); 97 this.tagname=localName; 98 if(tagname.equals("resource")) 99 { 100 mp3info=new Mp3Info(); 101 } 102 } 103 104 }
layout xml文件配合listView
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientation="vertical" android:layout_width="fill_parent" 3 android:layout_height="fill_parent"> 4 <LinearLayout android:id="@+id/listLinearLayout" 5 android:layout_width="fill_parent" android:layout_height="wrap_content" 6 android:orientation="vertical"> 7 <ListView android:id="@id/android:list" android:layout_width="fill_parent" 8 android:layout_height="wrap_content" android:drawSelectorOnTop="true" 9 android:scrollbars="vertical" /> 10 </LinearLayout> 11 </LinearLayout>
在Manifest中添加
<uses-permission android:name="android.permission.INTERNET" />
自信与努力 用心坚持