// 请求服务器获取xml文件
public String getMusicXML(String musicName, String singerName) {
try {
musicName = URLEncoder.encode(musicName,"gbk");
singerName = URLEncoder.encode(singerName,"gbk");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String strUrl = "http://box.zhangmen.baidu.com/x?op=12&count=1&title="
+ musicName + "$$" + singerName + "$$$$";
Log.e("url", strUrl);
try {
url = new URL(strUrl);
} catch (Exception e1) {
e1.printStackTrace();
}
BufferedReader br = null;
String s;
try {
InputStreamReader in = new InputStreamReader(url.openStream());
Log.e("the encode is ", in.getEncoding());
br = new BufferedReader(in);
} catch (IOException e1) {
Log.e("tag", "br is null");
}
try {
while ((s = br.readLine()) != null) {
sb.append(s + "\r\n");
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("sb=", sb.toString());
return sb.toString();
}
获得的xml文件如下:
引用
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<result>
<count>1</count>
<url>
<encode>
<![CDATA[
http://zhangmenshiting.baidu.com/data2/music/8453675/aWZoZ2ttbGaeomZzrZmmnJZva2RlmGluYWtsaWuYanBoY5ZrZ2mYb2pnlWpsbJtsaWVZoZ6adGhfamdpaGpuaGdpbGZqb244
]]>
</encode>
<decode>
<![CDATA[
8453675.mp3?xcode=800b2609956b3871c723a795b676d483&mid=0.84534706792597
]]>
</decode>
<type>8</type>
<lrcid>14706</lrcid>
<flag>1</flag>
</url>
<durl>
<encode>
<![CDATA[
http://zhangmenshiting2.baidu.com/data2/music/7339015/aGVmbWVnbGaeomZzrZmmnJZva2RlmGluYWtsaWuYanBoY5ZrZ2mYb2pnlWpsbJtsaWVZoZ6adGhfamdpaGpuaGdpbGZqb244
]]>
</encode>
<decode>
<![CDATA[
7339015.mp3?xcode=800b2609956b3871c723a795b676d483&mid=0.84534706792597
]]>
</decode>
<type>8</type>
<lrcid>14706</lrcid>
<flag>1</flag>
</durl>
<p2p>
<hash>f87e8f12130d017ab33622e2be5a444cbf3ae24a</hash>
<url>
<![CDATA[
http://zhangmenshiting.baidu.com/data2/music/5219955/5219955.mp3?xcode=800b2609956b3871c723a795b676d483
]]>
</url>
<type>mp3</type>
<size>1857593</size>
<bitrate>64</bitrate>
</p2p>
</result>
解析xml文件,获得String musicPath String lrcPath:
// 解析xml文件,得到music文件和lrc文件的地址
public String getLyricPath() {
int begin = 0, end = 0, number = 0;
String lrcPath = "";
begin = sb.indexOf("<lrcid>");
Log.e("test", "sb = " + sb);
if (begin != -1) {
end = sb.indexOf("</lrcid>", begin);
lrcPath = sb.substring(begin + 7, end);
number = Integer.parseInt(lrcPath);
}
lrcPath = "http://box.zhangmen.baidu.com/bdlrc/" + number / 100
+ "/" + number + ".lrc";
Log.e("test", "lrcPath = " + lrcPath);
return lrcPath;
}
public String getMusicPath(){
int begin = 0, end = 0;
String musicPath = "";
begin = sb.indexOf("<p2p>");
if(begin != -1){
end = sb.indexOf("</p2p>", begin);
musicPath = sb.substring(begin, end + 5);
}
begin = musicPath.indexOf("http://");
if (begin != -1) {
end = musicPath.indexOf("]]>");
musicPath = musicPath.substring(begin, end);
}
Log.e("test", "musicPath = " + musicPath);
return musicPath;
}
得到的musicPath = "http://zhangmenshiting.baidu.com/data2/music/5219955/5219955.mp3?xcode=de853ab52489ef65d21c0ed7334abdcd"
lrcPaht = "http://box.zhangmen.baidu.com/bdlrc/147/14706.lrc"
其中musicPaht会改变,lrcPath不变