在Android上使用酷狗歌詞API
阿新 • • 發佈:2017-12-26
ets 部分 tiff bject class clas down origin exception
參考自http://blog.csdn.net/u010752082/article/details/50810190
代碼先貼出來:
1 public void searchLyric(){ 2 final String name = musicName.getText().toString(); 3 final String duration = musicDuration.getText().toString(); 4 new Thread(new Runnable() { 5 @Override 6 public void run() {7 try { 8 //建立連接 -- 查找歌曲 9 String urlStr = "http://lyrics.kugou.com/search?ver=1&man=yes&client=pc&keyword=" + name + "&duration=" + duration + "&hash="; 10 URL url = new URL(encodeUrl(urlStr)); //字符串進行URL編碼 11 HttpURLConnection conn = (HttpURLConnection) url.openConnection();12 conn.connect(); 13 14 //讀取流 -- JSON歌曲列表 15 InputStream input = conn.getInputStream(); 16 String res = FileUtil.formatStreamToString(input); //流轉字符串 17 18 JSONObject json1 = new JSONObject(res); //字符串讀取為JSON 19 JSONArray json2 = json1.getJSONArray("candidates");20 JSONObject json3 = json2.getJSONObject(0); 21 22 //建立連接 -- 查找歌詞 23 urlStr = "http://lyrics.kugou.com/download?ver=1&client=pc&id=" + json3.get("id") + "&accesskey=" + json3.get("accesskey") + "&fmt=lrc&charset=utf8"; 24 url = new URL(encodeUrl(urlStr)); 25 conn = (HttpURLConnection) url.openConnection(); 26 conn.connect(); 27 28 //讀取流 -- 歌詞 29 input = conn.getInputStream(); 30 res = FileUtil.formatStreamToString(input); 31 JSONObject json4 = new JSONObject(res); 32 33 //獲取歌詞base64,並進行解碼 34 String base64 = json4.getString("content"); 35 final String lyric = Base64.getFromBASE64(base64); 36 37 Log.i("lyric", lyric); 38 39 runOnUiThread(new Runnable() { 40 @Override 41 public void run() { 42 showLyric.setText(lyric); 43 } 44 }); 45 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 } 50 }).start(); 51 }
首先說明一下,要搜索到歌詞,需要先搜索歌曲,得到歌曲對應的id和accesskey後才能進行歌詞的獲取。
那麽我們先從搜索歌曲的URL開始說起:
String urlStr = "http://lyrics.kugou.com/search?ver=1&man=yes&client=pc&keyword=" + name + "&duration=" + duration + "&hash=";
其中的name為搜索條件,最好為文件名或者“歌手 - 標題”的形式,搜索比較準確。duration為歌曲時長,單位毫秒。
記得要先對字符串鏈接進行URL編碼。
讀取流並轉換為字符串:
InputStream input = conn.getInputStream(); String res = FileUtil.formatStreamToString(input); //流轉字符串
接收到的數據res是這樣的:
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": 10 "C3D2BF9DD8A47A3FFB622B660D820B8D", "parinfo":[],"origiuid":"0", "score":60, "hitlayer": 11 7, "duration":227000, "sounduid":"0", "song":"Impossible", "uid":"410927974", "transuid": 12 "0", "transname":"", "adjust":0, "id":"22422076", "singer":"M?ns Zelmerl?w", "language":"" 13 }, 14 15 { 16 "soundname":"", "krctype":2, "nickname":"", "originame":"", "accesskey": 17 "F92BD21B377150B8F3C67B2A034D6FE0", "parinfo":[],"origiuid":"0", "score":50, "hitlayer": 18 7, "duration":227000, "sounduid":"0", "song":"Impossible", "uid":"486959192", "transuid": 19 "0", "transname":"", "adjust":0, "id":"19445996", "singer":"The Top Hits Band", "language": 20 "" 21 }, 22 23 { 24 "soundname":"", "krctype":2, "nickname":"", "originame":"", "accesskey": 25 "2B6A8E1CD4B59F475E28F2AF811F59E9", "parinfo":[],"origiuid":"0", "score":40, "hitlayer": 26 7, "duration":226750, "sounduid":"0", "song":"Impossible", "uid":"410927974", "transuid": 27 "0", "transname":"", "adjust":0, "id":"19201245", "singer":"Shontelle", "language":"" 28 }, 29 30 { 31 "soundname":"", "krctype":2, "nickname":"", "originame":"", "accesskey": 32 "D5B9AD83A10659CE2DAAD618C934F382", "parinfo":[],"origiuid":"0", "score":30, "hitlayer": 33 7, "duration":227000, "sounduid":"0", "song":"Impossible", "uid":"486958479", "transuid": 34 "0", "transname":"", "adjust":0, "id":"19160542", "singer":"The Top Hits Band", "language": 35 "" 36 }, 37 38 { 39 "soundname":"", "krctype":2, "nickname":"", "originame":"", "accesskey": 40 "27C664BC593E1B60D486E34AE479EFE7", "parinfo":[],"origiuid":"0", "score":20, "hitlayer": 41 7, "duration":227000, "sounduid":"0", "song":"Impossible", "uid":"486953482", "transuid": 42 "0", "transname":"", "adjust":0, "id":"18918409", "singer":"Tiffany Evans", "language":"" 43 }
44 ] 45 }
我們可以用new JSONObject()將字符串轉換為JSON對象,再取出candidates部分
JSONObject json1 = new JSONObject(res); //字符串讀取為JSON JSONArray json2 = json1.getJSONArray("candidates");
現在json2就是一個搜索到的歌曲集合了,我們一般取第一個結果,是比較精確的:
JSONObject json3 = json2.getJSONObject(0);
好的,現在我們已經獲取到了歌曲信息,接下來就是通過歌曲信息搜索歌詞。
搜索歌詞的URL需要兩個參數,id和accesskey,都可以從剛剛取到的歌曲信息json3中得到:
urlStr = "http://lyrics.kugou.com/download?ver=1&client=pc&id=" + json3.get("id") + "&accesskey=" + json3.get("accesskey") + "&fmt=lrc&charset=utf8";
進行連接後,我們就能獲取到歌詞的相關數據了:
其中content就是歌詞的內容,但是我們發現是經過base64編碼過的,我們需要對其進行解碼:
//讀取流 -- 歌詞 input = conn.getInputStream(); res = FileUtil.formatStreamToString(input); JSONObject json4 = new JSONObject(res); //獲取歌詞base64,並進行解碼 String base64 = json4.getString("content"); String lyric = Base64.getFromBASE64(base64);
最後lyric就是我們解碼後的歌詞了:
到這裏基本就結束了。
服務器返回的值有可能是空的,由於時間關系在這裏就不寫判斷了。
流轉String、字符串URL編碼、base64解碼都可以在網上找到,代碼比較多這裏就不貼出來了。
candidates
在Android上使用酷狗歌詞API