• Android获取百度音乐下载音乐和歌词下载链接


    首先,你必须通过以下连接下载歌曲:

    http://box.zhangmen.baidu.com/x?op=12&count=1&title={title}$${author}$$$$

    能够获取一个xml文件通过解析xml文件就能够读取到音乐下载的地址和歌词下载的地址。

    部分代码:

      String title = "六月的雨";
      String singer = "胡歌";                    
      String url = "http://box.zhangmen.baidu.com/x?op=12&count=1&title={title}$${author}$$$$"
      url=url.replace("{title}", URLEncoder.encode(title, "utf-8"));    
       //将String字符串转码
      if(singer == null) {
    url=url.replace("$${author}$$$$", "");
     }else {
    url=url.replace("{author}", URLEncoder.encode(singer, "utf-8"));
     }



    以下这部分是依据url地址,下载xml文件的代码:

     public static InputStream downLoad(String urlStr) {   //依据网络地址获取输入流
    InputStream inputStream = null;
        try {
        URL url = new URL(urlStr);
        HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
        urlConn.setRequestMethod("GET");
        urlConn.setConnectTimeout(10000);
        urlConn.setDoOutput(true);
        urlConn.setDoInput(true);
        //urlConn.setUseCaches(true);
        urlConn.connect();
        System.out.println("UrlCode:"+urlConn.getResponseCode());
        if(urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
        System.out.println("请求失败");
        }else {
        System.out.println("请求成功  ");
        }
        inputStream = urlConn.getInputStream();
        }catch(Exception e) {
        e.printStackTrace();
        }
        return inputStream;
       }

     public static String downLoadTextFile(String url) {              
    StringBuffer sb = new StringBuffer();
    BufferedReader buffer = null;
    InputStream instream = null;
    String line=null;
    try {
    instream = downLoad(url);
    buffer = new BufferedReader(new InputStreamReader(instream,"GB2312"));
    while((line = buffer.readLine()) != null){
    System.out.println(line);
    sb.append(line+" ");
    }
    }catch(IOException e) {
    e.printStackTrace();
    }finally {
    try {
    buffer.close();
    instream.close();
    }catch(IOException e) {
    e.printStackTrace();
    }
    }
    return sb.toString();
    }

    下载的XML文件例如以下:

    <<result>
    <count>1</count>
    <url>
    <encode>
    <![CDATA[
    http://zhangmenshiting.baidu.com/data2/music/35447544/ZGdnaGxra2xfn6NndK6ap5WXcJppZ2ppZ2aWaWaXnWthlJSWmphucGJklZaWmJiakmlplpZuZ5xkaZZramdtbGFmbFqin5t1YWBnamVtcGhpaGhpa21rcTE$
    ]]>
    </encode>
    <decode>
    <![CDATA[
    35447544.mp3?xcode=f413164c51af30babeb7812bbababa76ba80d37c75164049&mid=0.46079086556749
    ]]>
    </decode>
    <type>8</type>
    <lrcid>35220</lrcid>
    <flag>1</flag>
    </url>
    <durl>
    <encode>
    <![CDATA[
    http://zhangmenshiting2.baidu.com/data2/music/35447547/ZGdnaGxra29fn6NndK6ap5WXcJppZ2ppZ2aWaWaXnWthlJSWmphucGJklZaWmJiakmlpZZaYZ2pnY2xobm1wbpOTaFqin5t1YWBnamVtcGhpaGhpa21rcTE$
    ]]>
    </encode>
    <decode>
    <![CDATA[
    35447547.mp3?

    xcode=f413164c51af30babeb7812bbababa761ab0261949796ba5&mid=0.46079086556749
    ]]>
    </decode>
    <type>8</type>
    <lrcid>35220</lrcid>
    <flag>1</flag>
    </durl>
    <p2p>
    <hash>9d1c20c4d3055f25acb6b61d727cafcfae096519</hash>
    <url>
    <![CDATA[ ]]>
    </url>
    <type>mp3</type>
    <size>5511091</size>
    <bitrate>192</bitrate>
    </p2p>
    </result>


    当中的count值为1是说返回的是一个,,encode里的值是歌曲加密后的地址,加密仅仅是对文件名称加密 的,

    我们须要的仅仅是前面的路径,也就是 http://zhangmenshiting.baidu.com/data2/music/35447544/
    这部分,然后复制decode 的值:     

    35447544.mp3?

    xcode=f413164c51af30babeb7812bbababa76ba80d37c75164049&mid=0.46079086556749

    把他们连接起来就是mp3的url下载地址:  

    http://zhangmenshiting.baidu.com/data2/music/35447544/35447544.mp3?xcode=f413164c51af30babeb7812bbababa76ba80d37c75164049&mid=0.46079086556749

    http://box.zhangmen.baidu.com/bdlrc/ 这个是百度lrc歌词存放地址,
    xml中的lrcid就是地址可是要把它转换一下。首先
    35220/100 =352.20 取小于等于352.20 的最大整数就是352,于是这首歌完整的歌词地址就出来了:
    http://box.zhangmen.baidu.com/bdlrc/352/35220.lrc 

    这部分也是网上看到的,能够到网上查到的。好像就是这么个规则


    以下是解析XML文件:

    //使用dom解析xml文件
    public static String parasXML(InputStream inputStream) {
    try {
    String lrcid = null,url = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document dom = builder.parse(inputStream);
    Element root =  dom.getDocumentElement();
    Node count = root.getFirstChild();
    NodeList items = root.getElementsByTagName("url");
    if(items.getLength() <= 0) {
    System.out.println("歌曲未找到");
    return null;
    }else {
    Element urlNode = (Element)items.item(0);
    //System.out.println(urlNode.getTextContent());
    Element lrcidNode = (Element)(urlNode.getElementsByTagName("lrcid").item(0));
    Element encodeNode = (Element)urlNode.getElementsByTagName("encode").item(0);
    Element decodeNode = (Element)urlNode.getElementsByTagName("decode").item(0);
    if(encodeNode == null) {
    System.out.println("歌曲信息为空,无法下载");
    return null;
    }else {
    System.out.println("encode="+encodeNode.getTextContent());
    }
    if(lrcidNode == null) {
    System.out.println("lrcid == null");
    }else {
    System.out.println(lrcidNode.getNodeName());
    }
    if("lrcid".equals(lrcidNode.getNodeName())) {
    System.out.println(lrcidNode.getTextContent());
    lrcid = lrcidNode.getTextContent();
    }else {
    System.out.println(lrcidNode.getNodeName());
    }
    //获取encode里的值
    String temp1 = encodeNode.getTextContent();   
    //获取decodeNode的值
    String temp2 = decodeNode.getTextContent();                                   
    StringBuffer buffer = new StringBuffer();

    //接下来是将temp1和temp2进行切割解码合并在一起
    String [] arrayTemp1 = temp1.split("/"); 
    for(int i=0;i<arrayTemp1.length-1;i++) {
    buffer.append(arrayTemp1[i]+"/");
    }
    //把解码的地址合在一起
    buffer.append(temp2);                                                    
    url = buffer.toString();
    url = url + "##"+ lrcid;
    System.out.println("xml:url="+url);
    return url;
    }
    }catch(Exception e) {
    e.printStackTrace();
    return null;
    }
    }

    后面就能够通过##来分开url和lrcid的值从而获取对应的值

    下载文件的就能够先获取InputStream然后通过这个流接口获取对应的值













    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    JVM内存模型
    052-224(新增70题2018)
    052-223(新增70题2018)
    052-222(新增70题2018)
    052-221(新增70题2018)
    052-220(新增70题2018)
    052-219(新增70题2018)
    052-218(新增70题2018)
    052-217(新增70题2018)
    052-216(新增70题2018)
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4734844.html
Copyright © 2020-2023  润新知