在Android上使用酷狗歌词API

参考自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 }

首先说明一下,要搜索到歌词,需要先搜索歌曲,得到歌曲对应的idaccesskey后才能进行歌词的获取。

那么我们先从搜索歌曲的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需要两个参数,idaccesskey,都可以从刚刚取到的歌曲信息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

转载于:https://www.cnblogs.com/xiu2017/p/8117335.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值