Android get Baidu music download address and lyrics download link

Source: Internet
Author: User

First through the songs you want to download through the following connections:

http://box.zhangmen.baidu.com/x?op=12&count=1&title={title}$${author}$$$$

You can get an XML file that can be read to the address of the music download and the lyrics download address by parsing the XML file.

Part of the code:

String title = "Rain of June";
String singer = "Hu";
String url = "http://box.zhangmen.baidu.com/x?op=12&count=1&title={title}$${author}$$$$"
Url=url.replace ("{title}", Urlencoder.encode (title, "Utf-8"));
Transcode A String string
if (singer = = null) {
Url=url.replace ("$${author}$$$$", "" ");
}else {
Url=url.replace ("{author}", Urlencoder.encode (singer, "Utf-8"));
}



The following section is the code for downloading the XML file based on the URL address:

public static InputStream DownLoad (String urlstr) {Get input stream based on network address
InputStream inputstream = null;
try {
URL url = new URL (urlstr);
HttpURLConnection urlconn = (httpurlconnection) url.openconnection ();
Urlconn.setrequestmethod ("GET");
Urlconn.setconnecttimeout (10000);
Urlconn.setdooutput (TRUE);
Urlconn.setdoinput (TRUE);
Urlconn.setusecaches (TRUE);
Urlconn.connect ();
System.out.println ("Urlcode:" +urlconn.getresponsecode ());
if (Urlconn.getresponsecode ()! = HTTPURLCONNECTION.HTTP_OK) {
SYSTEM.OUT.PRINTLN ("request Failed");
}else {
SYSTEM.OUT.PRINTLN ("request succeeded");
}
InputStream = Urlconn.getinputstream ();
}catch (Exception e) {
E.printstacktrace ();
}
return inputstream;
}

public static string Downloadtextfile (string url) {
StringBuffer sb = new StringBuffer ();
BufferedReader buffer = null;
InputStream instream = null;
String Line=null;
try {
instream = downLoad (URL);
Buffer = new BufferedReader (new InputStreamReader (instream, "GB2312"));
while (line = Buffer.readline ()) = null) {
System.out.println (line);
Sb.append (line+ "\ n");
}
}catch (IOException e) {
E.printstacktrace ();
}finally {
try {
Buffer.close ();
Instream.close ();
}catch (IOException e) {
E.printstacktrace ();
}
}
return sb.tostring ();
}

The downloaded XML file is as follows:

<<result>
<count>1</count>
<url>
<encode>
<! [cdata[
http://zhangmenshiting.baidu.com/data2/music/35447544/Zgdnagxra2xfn6nndk6ap5wxcjppz2ppz2awawaxnwthljswmphucgjklzawmjiakmlplpzuz5xkazzramdtbgfmbfqin5t1ywbnamvtcghpaghpa21rcte $
]]>
</encode>
<decode>
<! [cdata[
35447544.mp3?xcode=f413164c51af30babeb7812bbababa76ba80d37c75164049&mid=0.46079086556749
]]>
</decode>
<type>8</type>
<lrcid>35220</lrcid>
<flag>1</flag>
</url>
<durl>
<encode>
<! [cdata[
http://zhangmenshiting2.baidu.com/data2/music/35447547/ Zgdnagxra29fn6nndk6ap5wxcjppz2ppz2awawaxnwthljswmphucgjklzawmjiakmlpzzayz2pny2xobm1wbpotafqin5t1ywbnamvtcghpaghpa21rcte $
]]>
</encode>
<decode>
<! [cdata[
35447547.mp3?xcode=f413164c51af30babeb7812bbababa761ab0261949796ba5&mid=0.46079086556749
]]>
</decode>
<type>8</type>
<lrcid>35220</lrcid>
<flag>1</flag>
</durl>
<p2p>

<url>
<! [Cdata[]]>
</url>
<type>mp3</type>
<size>5511091</size>
<bitrate>192</bitrate>
</p2p>
</result>


where the count value of 1 is said to return is one, the value in encode is the address after the song is encrypted, encryption simply encrypts the file name,

All we need is the front path, which is http://zhangmenshiting.baidu.com/data2/music/35447544/.
This section, and then copy the value of the decode:

35447544.mp3?xcode=f413164c51af30babeb7812bbababa76ba80d37c75164049&mid=0.46079086556749

Linking them up is the MP3 URL:

http://zhangmenshiting.baidu.com/data2/ music/35447544/35447544.mp3?xcode=f413164c51af30babeb7812bbababa76ba80d37c75164049&mid=0.46079086556749

http://box.zhangmen.baidu.com/bdlrc/ This is the Baidu LRC lyrics storage address,
35220/100 =352.20 is less than or equal to 352.20 the largest integer is 352, So the full lyrics address of the song came out:
http://box.zhangmen.baidu.com/bdlrc/352 /35220 . lrc 

This part is also seen on the internet, can be found on the internet, it seems to be such a rule


Here is the parse XML file:

Parsing an XML file using the DOM
public static String Parasxml (InputStream inputstream) {
try {
String lrcid = Null,url = null;
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder builder = Factory.newdocumentbuilder ();
Document dom = Builder.parse (InputStream);
Element root = Dom.getdocumentelement ();
Node count = Root.getfirstchild ();
NodeList items = root.getelementsbytagname ("url");
if (items.getlength () <= 0) {
System.out.println ("Song Not Found");
return null;
}else {
Element Urlnode = (element) items.item (0);
System.out.println (Urlnode.gettextcontent ());
Element Lrcidnode = (Element) (Urlnode.getelementsbytagname ("Lrcid"). Item (0));
Element Encodenode = (Element) urlnode.getelementsbytagname ("Encode"). Item (0);
Element Decodenode = (Element) urlnode.getelementsbytagname ("Decode"). Item (0);
if (Encodenode = = null) {
System.out.println ("The song information is empty and cannot be downloaded");
return null;
}else {
System.out.println ("encode=" +encodenode.gettextcontent ());
}
if (Lrcidnode = = null) {
System.out.println ("lrcid = = null");
}else {
System.out.println (Lrcidnode.getnodename ());
}
if ("Lrcid". Equals (Lrcidnode.getnodename ())) {
System.out.println (Lrcidnode.gettextcontent ());
Lrcid = Lrcidnode.gettextcontent ();
}else {
System.out.println (Lrcidnode.getnodename ());
}
Get the value in encode
String Temp1 = Encodenode.gettextcontent ();
Gets the value of the Decodenode
String Temp2 = Decodenode.gettextcontent ();
StringBuffer buffer = new StringBuffer ();

The next step is to combine Temp1 and TEMP2 with split decoding.
String [] arrayTemp1 = Temp1.split ("/");
for (int i=0;i<arraytemp1.length-1;i++) {
Buffer.append (arraytemp1[i]+ "/");
}
Put the decoded addresses together.
Buffer.append (TEMP2);
url = buffer.tostring ();
url = url + "# #" + lrcid;
System.out.println ("xml:url=" +url);
return URL;
}
}catch (Exception e) {
E.printstacktrace ();
return null;
}
}

You can go through # #来分开url和lrcid的值从而获取相应的值

Download the file can get InputStream and then through this stream interface to get the corresponding value













Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.