在Android上使用酷狗歌詞API

來源:互聯網
上載者:User

標籤:sdn   url編碼   class   inpu   net   pen   form   接下來   字串轉換   

參考自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);  //字串讀取為JSON19                 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                     @Override41                     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);  //字串讀取為JSONJSONArray 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.