Reference from http://blog.csdn.net/u010752082/article/details/50810190
The code is posted first:
1 Public voidsearchlyric () {2 FinalString name =Musicname.gettext (). toString ();3 FinalString Duration =Musicduration.gettext (). toString ();4 NewThread (NewRunnable () {5 @Override6 Public voidrun () {7 Try {8 //Establish Connection--Find songs9String urlstr = "http://lyrics.kugou.com/search?ver=1&man=yes&client=pc&keyword=" + name + "&duration = "+ Duration +" &hash= ";TenURL url =NewURL (Encodeurl (URLSTR));//URL encoding of strings OneHttpURLConnection conn =(HttpURLConnection) url.openconnection (); A Conn.connect (); - - //Read stream-JSON song list theInputStream input =Conn.getinputstream (); -String res = fileutil.formatstreamtostring (input);//Flow String - -Jsonobject Json1 =NewJsonobject (RES);//string read as JSON +Jsonarray Json2 = Json1.getjsonarray ("candidates"); -Jsonobject Json3 = json2.getjsonobject (0); + A //Establish a connection--find lyrics atUrlstr = "http://lyrics.kugou.com/download?ver=1&client=pc&id=" + json3.get ("id") + "&accesskey=" + Json3.get ("accesskey") + "&fmt=lrc&charset=utf8"; -URL =NewURL (Encodeurl (URLSTR)); -conn =(HttpURLConnection) url.openconnection (); - Conn.connect (); - - //Read stream-lyrics ininput =Conn.getinputstream (); -res =fileutil.formatstreamtostring (input); toJsonobject Json4 =NewJsonobject (res); + - //get the lyrics base64 and decode them theString base64 = json4.getstring ("Content"); * FinalString lyric =Base64.getfrombase64 (base64); $ Panax NotoginsengLOG.I ("lyric", lyric); - theRunonuithread (NewRunnable () { + @Override A Public voidrun () { the Showlyric.settext (lyric); + } - }); $ $}Catch(Exception e) { - e.printstacktrace (); - } the } - }). Start ();Wuyi}
First of all, to search for lyrics, you need to search for songs, to get the songs corresponding to the ID and accesskey after the lyrics can be obtained.
Let's start with the URL of the search song :
name duration + "&hash=";
The name is the search criteria, preferably the file name or "singer-title" form, the search is more accurate. Duration is the length of the song, in milliseconds.
Remember to first URL-encode the string link.
Read the stream and convert to a string:
InputStream input == fileutil.formatstreamtostring (input); // Flow String
The received data res is this:
1{"Ugccandidates":[],2"UGC": 0,3"Info": "OK",4"Status": 200,5"Proposal": "22422076",6"Keyword": "Impossible",7"Candidates":[8 {9"Soundname": "", "Krctype": 2, "nickname": "", "Originame": "", "accesskey":Ten"c3d2bf9dd8a47a3ffb622b660d820b8d", "Parinfo": [], "Origiuid": "0", "Score": "Hitlayer": One7, "duration": 227000, "Sounduid": "0", "Song": "Impossible", "UID": "410927974", "Transuid": A"0", "transname": "", "Adjust": 0, "id": "22422076", "Singer": "M?ns zelmerl?w", "Language": "" - }, - the { -"Soundname": "", "Krctype": 2, "nickname": "", "Originame": "", "accesskey": -"F92bd21b377150b8f3c67b2a034d6fe0", "Parinfo": [], "Origiuid": "0", "Score": "Hitlayer": -7, "duration": 227000, "Sounduid": "0", "Song": "Impossible", "UID": "486959192", "Transuid": +"0", "transname": "", "Adjust": 0, "id": "19445996", "Singer": "The Top Hits Band", "language": -"" + }, A at { -"Soundname": "", "Krctype": 2, "nickname": "", "Originame": "", "accesskey": -"2b6a8e1cd4b59f475e28f2af811f59e9", "Parinfo": [], "Origiuid": "0", "score": +, "Hitlayer": -7, "duration": 226750, "Sounduid": "0", "Song": "Impossible", "UID": "410927974", "Transuid": -"0", "transname": "", "Adjust": 0, "id": "19201245", "Singer": "Shontelle", "Language": "" - }, in - { to"Soundname": "", "Krctype": 2, "nickname": "", "Originame": "", "accesskey": +"d5b9ad83a10659ce2daad618c934f382", "Parinfo": [], "Origiuid": "0", "score": +, "Hitlayer": -7, "duration": 227000, "Sounduid": "0", "Song": "Impossible", "UID": "486958479", "Transuid": the"0", "transname": "", "Adjust": 0, "id": "19160542", "Singer": "The Top Hits Band", "language": *"" $ },Panax Notoginseng - { the"Soundname": "", "Krctype": 2, "nickname": "", "Originame": "", "accesskey": +"27c664bc593e1b60d486e34ae479efe7", "Parinfo": [], "Origiuid": "0", "Score": "Hitlayer": A7, "duration": 227000, "Sounduid": "0", "Song": "Impossible", "UID": "486953482", "Transuid": the"0", "transname": "", "Adjust": 0, "id": "18918409", "Singer": "Tiffany Evans", "Language": "" + }
- ] $}
We can convert a string to a JSON object with new Jsonobject (), and then take out the candidates section
New Jsonobject (res); // string read as JSON Jsonarray Json2 = Json1.getjsonarray ("candidates");
Now Json2 is a collection of songs to search, we generally take the first result, is more accurate:
Jsonobject Json3 = json2.getjsonobject (0);
OK, now that we've got the song information, the next step is to search for the lyrics through the song information.
searching for the lyrics URL requires two parameters,ID and accesskey, which can be obtained from the song information Json3 just taken:
json3.get ("id") json3.get ("accesskey") + "&fmt=lrc&charset=utf8";
After the connection, we can get the relevant data of the lyrics:
The content is the contents of the lyrics, but we find that it has been encoded by Base64, we need to decode it:
// Read stream-lyrics input ==new jsonobject (res); // get the lyrics base64 and decode them String base64 = json4.getstring ("content"); String lyric = Base64.getfrombase64 (base64);
The last lyric is the lyrics we decoded:
This is the end of the basic.
The value returned by the server may be empty because the time relationship is not written here.
Flow string, string URL encoding, base64 decoding can be found on the Internet, the code is more than here is not posted out.
Candidates
Use the cool Dog lyrics API on Android