在Android上使用酷狗歌词API

博观而约取,厚积而薄发。这篇文章主要讲述在Android上使用酷狗歌词API相关的知识,希望能为你提供帮助。
参考自http://blog.csdn.net/u010752082/article/details/50810190
代码先贴出来:

1 public void searchLyric(){ 2final String name = musicName.getText().toString(); 3final String duration = musicDuration.getText().toString(); 4new Thread(new Runnable() { 5@Override 6public void run() { 7try { 8//建立连接 -- 查找歌曲 9String urlStr = "http://lyrics.kugou.com/search?ver=1& man=yes& client=pc& keyword=" + name + "& duration=" + duration + "& hash="; 10URL url = new URL(encodeUrl(urlStr)); //字符串进行URL编码 11HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 12conn.connect(); 13 14//读取流 -- JSON歌曲列表 15InputStream input = conn.getInputStream(); 16String res = FileUtil.formatStreamToString(input); //流转字符串 17 18JSONObject json1 = new JSONObject(res); //字符串读取为JSON 19JSONArray json2 = json1.getJSONArray("candidates"); 20JSONObject json3 = json2.getJSONObject(0); 21 22//建立连接 -- 查找歌词 23urlStr = "http://lyrics.kugou.com/download?ver=1& client=pc& id=" + json3.get("id") + "& accesskey=" + json3.get("accesskey") + "& fmt=lrc& charset=utf8"; 24url = new URL(encodeUrl(urlStr)); 25conn = (HttpURLConnection) url.openConnection(); 26conn.connect(); 27 28//读取流 -- 歌词 29input = conn.getInputStream(); 30res = FileUtil.formatStreamToString(input); 31JSONObject json4 = new JSONObject(res); 32 33//获取歌词base64,并进行解码 34String base64 = json4.getString("content"); 35final String lyric = Base64.getFromBASE64(base64); 36 37Log.i("lyric", lyric); 38 39runOnUiThread(new Runnable() { 40@Override 41public void run() { 42showLyric.setText(lyric); 43} 44}); 45 46} catch (Exception e) { 47e.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": 117, "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": 187, "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": 267, "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": 337, "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": 417, "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";

进行连接后,我们就能获取到歌词的相关数据了:
在Android上使用酷狗歌词API

文章图片

其中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就是我们解码后的歌词了:
在Android上使用酷狗歌词API

文章图片

到这里基本就结束了。
服务器返回的值有可能是空的,由于时间关系在这里就不写判断了。
流转String、字符串URL编码、base64解码都可以在网上找到,代码比较多这里就不贴出来了。
 
 
【在Android上使用酷狗歌词API】 
candidates



    推荐阅读