实现了从服务器当中下载XML文件并且解析XML文件。
package guoximing.download;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;//看的出来 IOException 是MalformedURLException 的父类public class HttpDownloader {private URL url = null;public String download(String urlStr) {StringBuffer sb = new StringBuffer();String line = null;BufferedReader buffer = null;try {// 创建一个URL对象URL url = new URL(urlStr);// 创建一个Http连接HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();// 使用IO流读取数据buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));while ((line = buffer.readLine()) != null) {sb.append(line);}} catch (Exception e) {e.printStackTrace();} finally {try {buffer.close();} catch (Exception e) {e.printStackTrace();}}return sb.toString();}
}
package guoximing.mp3player;import guoximing.download.HttpDownloader;
import guoximing.model.Mp3Info;
import guoximing.xml.Mp3ListContentHandler;import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleAdapter;import com.example.mp3player.R;@SuppressLint("NewApi")
public class MP3ListActivity extends ListActivity{private static final int UPDATE = 1;private static final int ABOUT = 2;@Overrideprotected void onCreate(Bundle savedInstanceState) {//详见StrictMode文档StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork() // or .detectAll() for all detectable problems.penaltyLog().build());StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(0,UPDATE,1,R.string.mp3List_update);menu.add(0,ABOUT,2,R.string.mp3List_about);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if(UPDATE == item.getItemId()){System.out.println(UPDATE);String str = "http://121.49.87.4:8080/mp3/resources.xml";String xml = downloaderXML(str);System.out.println("xml....."+xml);parse(xml);}else{System.out.println(ABOUT);}return super.onOptionsItemSelected(item);}private String downloaderXML(String urlStr){HttpDownloader httpDownloader = new HttpDownloader();String result = null;result = httpDownloader.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);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){ }return infos;}
}
package guoximing.model;public class Mp3Info {private String id = null;private String mp3Name = null;private String mp3Size = null;private String lrcName = null;private String lrcSize = null;public Mp3Info(String id, String mp3Name, String mp3Size, String lrcName,String lrcSize) {super();this.id = id;this.mp3Name = mp3Name;this.mp3Size = mp3Size;this.lrcName = lrcName;this.lrcSize = lrcSize;}public Mp3Info() {super();}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getMp3Name() {return mp3Name;}public void setMp3Name(String mp3Name) {this.mp3Name = mp3Name;}public String getMp3Size() {return mp3Size;}public void setMp3Size(String mp3Size) {this.mp3Size = mp3Size;}public String getLrcName() {return lrcName;}public void setLrcName(String lrcName) {this.lrcName = lrcName;}public String getLrcSize() {return lrcSize;}public void setLrcSize(String lrcSize) {this.lrcSize = lrcSize;}@Overridepublic String toString() {return "Mp3Info [id=" + id + ", mp3Name=" + mp3Name + ", mp3Size="+ mp3Size + ", lrcName=" + lrcName + ", lrcSize=" + lrcSize+ "]";}}
package guoximing.xml;import guoximing.model.Mp3Info;import java.util.List;import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;public class Mp3ListContentHandler extends DefaultHandler{//每一个<resource>都是一个MP3Info对象private List<Mp3Info> infos = null;private Mp3Info mp3Info = null;private String tagName = null;public Mp3ListContentHandler(List<Mp3Info> infos) {super();this.infos = infos;}public List<Mp3Info> getInfos() {return infos;}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {String temp = new String(ch,start,length);if(tagName.equals("id")){mp3Info.setId(temp);}else if(tagName.equals("mp3.name")){mp3Info.setMp3Name(temp);}else if(tagName.equals("mp3.size")){mp3Info.setMp3Size(temp);}else if(tagName.equals("lrc.name")){mp3Info.setLrcName(temp);}else if(tagName.equals("lrc.size")){mp3Info.setLrcSize(temp);}super.characters(ch, start, length);}@Overridepublic void endDocument() throws SAXException {super.endDocument();}@Overridepublic void startDocument() throws SAXException {super.startDocument();}@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {this.tagName = localName;if(tagName.equals("resource")){mp3Info = new Mp3Info();}super.startElement(uri, localName, qName, attributes);}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {if(qName.equals("resource")){infos.add(mp3Info);}tagName = "";super.endElement(uri, localName, qName);}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><ListView android:id="@id/android:list" android:layout_width="fill_parent"android:layout_height="wrap_content" android:drawSelectorOnTop="false"android:scrollbars="vertical" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.mp3player"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><uses-permission android:name="android.permission.INTERNET"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="guoximing.mp3player.MP3ListActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>