Android网易云音乐能用的API

最近在做一个仿网易云音乐的APP,需要通过网络获取歌曲以及歌词,于是做了这些API整合,这里面的都是我测试过能够获取到信息的API,写这篇博客为了备份与分享。

先说API来源,来自这篇博客:网易云音乐常用API

我们使用Volley来解析处理网络请求,由于返回的是Json,所以我们使用Gson来解析返回数据。

实现

Volley下载以及使用地址:Volley使用指南以及下载

Gson.jar下载地址:Gson.jar

使用方法:

AS中拷贝类库文件到libs文件夹下,然后右键类库文件选择Add as Library。

下面附上工具类代码:

1.获取RequestQueue实例:

  1. package winter.zxb.smilesb101.testdemo;  
  2. import android.content.Context;  
  3.   
  4. import com.android.volley.Request;  
  5. import com.android.volley.RequestQueue;  
  6. import com.android.volley.Response;  
  7. import com.android.volley.toolbox.JsonObjectRequest;  
  8. import com.android.volley.toolbox.Volley;  
  9.   
  10. import org.json.JSONObject;  
  11.   
  12. /** 
  13.  * 网络通信工具类 
  14.  * Created by SmileSB101 on 2016/11/1 0001. 
  15.  */  
  16.   
  17. public class InternetUtil{  
  18.     /** 
  19.      * 网络请求队列 
  20.      */  
  21.     private static RequestQueue mRequestqueue;  
  22.     public static RequestQueue getmRequestqueue(Context context)  
  23.     {  
  24.         if(mRequestqueue == null)  
  25.         {  
  26.             mRequestqueue = Volley.newRequestQueue(context);  
  27.             return mRequestqueue;  
  28.         }  
  29.         else{  
  30.             return mRequestqueue;  
  31.         }  
  32.     }  
  33. }  
package winter.zxb.smilesb101.testdemo;
import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

/**
 * 网络通信工具类
 * Created by SmileSB101 on 2016/11/1 0001.
 */

public class InternetUtil{
    /**
     * 网络请求队列
     */
    private static RequestQueue mRequestqueue;
    public static RequestQueue getmRequestqueue(Context context)
    {
        if(mRequestqueue == null)
        {
            mRequestqueue = Volley.newRequestQueue(context);
            return mRequestqueue;
        }
        else{
            return mRequestqueue;
        }
    }
}

2.网络请求处理类

  1. package winter.zxb.smilesb101.testdemo;  
  2.   
  3. import android.content.Context;  
  4. import android.util.Log;  
  5. import android.util.StateSet;  
  6.   
  7. import com.android.volley.RequestQueue;  
  8. import com.android.volley.Response;  
  9. import com.android.volley.VolleyError;  
  10. import com.android.volley.toolbox.StringRequest;  
  11.   
  12. import org.json.JSONException;  
  13. import org.json.JSONObject;  
  14.   
  15.   
  16. /** 
  17.  * 音乐网络类 
  18.  * Created by SmileSB101 on 2016/11/1 0001. 
  19.  */  
  20.   
  21. public class MusicNetWork{  
  22.     /** 
  23.      * 网易音乐搜索API 
  24.      * http://s.music.163.com/search/get/ 
  25.      * 获取方式:GET 
  26.      * 参数: 
  27.      * src: lofter //可为空 
  28.      * type: 1 
  29.      * filterDj: true|false //可为空 
  30.      * s: //关键词 
  31.      * limit: 10 //限制返回结果数 
  32.      * offset: 0 //偏移 
  33.      * callback: //为空时返回json,反之返回jsonp callback 
  34.      * @param s 
  35.      * @param context 
  36.      * @return 
  37.      * 注意废数字才用‘’符号,要不不能用,否则出错!! 
  38.      */  
  39.     public static void SearchMusic(Context context,String s,int limit,int type,int offset){  
  40.         String url = UrlConstants.CLOUD_MUSIC_API_SEARCH + ”type=”+type+“&s=’” + s + “’&limit=”+limit+“&offset=”+offset;  
  41.         RequestQueue requestQueue = InternetUtil.getmRequestqueue(context);  
  42.         StringRequest straingRequest = new StringRequest(url,new Response.Listener<String>(){  
  43.             @Override  
  44.             public void onResponse(String s){  
  45.                 try {  
  46.                     JSONObject json = new JSONObject(s);  
  47.                     Log.i(”onResponse: ”,json.toString());  
  48.                 } catch(JSONException e) {  
  49.                     e.printStackTrace();  
  50.                 }  
  51.             }  
  52.         },new Response.ErrorListener(){  
  53.             @Override  
  54.             public void onErrorResponse(VolleyError volleyError){  
  55.                 Log.i(”onResponse: ”,volleyError.toString());  
  56.             }  
  57.         });  
  58.         requestQueue.add(straingRequest);  
  59.     }  
  60.   
  61.     /** 
  62.      * 网易云音乐歌曲信息API 
  63.      * @param context 
  64.      * @param id 歌曲id 
  65.      * @param ids 用[]包裹起来的歌曲id 写法%5B %5D 
  66.      * @return 
  67.      */  
  68.     public static void Cloud_Music_MusicInfoAPI(Context context,String id,String ids)  
  69.     {  
  70.         String url = UrlConstants.CLOUD_MUSIC_API_MUSICINGO + ”id=”+id+“&ids=%5B”+ids+“%5D”;  
  71.         RequestQueue requestQueue = InternetUtil.getmRequestqueue(context);  
  72.         StringRequest straingRequest = new StringRequest(url,new Response.Listener<String>(){  
  73.             @Override  
  74.             public void onResponse(String s){  
  75.                 try {  
  76.                     JSONObject json = new JSONObject(s);  
  77.                     Log.i(”onResponse: ”,json.toString());  
  78.                 } catch(JSONException e) {  
  79.                     e.printStackTrace();  
  80.                 }  
  81.             }  
  82.         },new Response.ErrorListener(){  
  83.             @Override  
  84.             public void onErrorResponse(VolleyError volleyError){  
  85.                 Log.i(”onResponse: ”,volleyError.toString());  
  86.             }  
  87.         });  
  88.         requestQueue.add(straingRequest);  
  89.     }  
  90.   
  91.     /** 
  92.      * 获取歌曲歌词的API 
  93.      *URL: 
  94.  
  95.      GET http://music.163.com/api/song/lyric 
  96.  
  97.      必要参数: 
  98.  
  99.      id:歌曲ID 
  100.  
  101.      lv:值为-1,我猜测应该是判断是否搜索lyric格式 
  102.  
  103.      kv:值为-1,这个值貌似并不影响结果,意义不明 
  104.  
  105.      tv:值为-1,是否搜索tlyric格式 
  106.      * @param context 
  107.      * @param os 
  108.      * @param id 
  109.      */  
  110.     public static void Cloud_Muisc_getLrcAPI(Context context,String os,String id)  
  111.     {  
  112.         String url = UrlConstants.CLOUD_MUSIC_API_MUSICLRC + ”os=”+os+“&id=”+id+“&lv=-1&kv=-1&tv=-1”;  
  113.         RequestQueue requestQueue = InternetUtil.getmRequestqueue(context);  
  114.         StringRequest straingRequest = new StringRequest(url,new Response.Listener<String>(){  
  115.             @Override  
  116.             public void onResponse(String s){  
  117.                 try {  
  118.                     JSONObject json = new JSONObject(s);  
  119.                     Log.i(”onResponse: ”,json.toString());  
  120.                 } catch(JSONException e) {  
  121.                     e.printStackTrace();  
  122.                 }  
  123.             }  
  124.         },new Response.ErrorListener(){  
  125.             @Override  
  126.             public void onErrorResponse(VolleyError volleyError){  
  127.                 Log.i(”onResponse: ”,volleyError.toString());  
  128.             }  
  129.         });  
  130.         requestQueue.add(straingRequest);  
  131.     }  
  132.   
  133.     /** 
  134.      * 获取歌单的API 
  135.      * @param context 
  136.      * @param id 歌单ID 
  137.      */  
  138.     public static void Cloud_Muisc_MusicListSearch(Context context,String id)  
  139.     {  
  140.         String url = UrlConstants.CLOUD_MUSIC_API_MUSICLIST + ”id=”+id+“&updateTime=-1”;  
  141.         RequestQueue requestQueue = InternetUtil.getmRequestqueue(context);  
  142.         StringRequest straingRequest = new StringRequest(url,new Response.Listener<String>(){  
  143.             @Override  
  144.             public void onResponse(String s){  
  145.                 try {  
  146.                     JSONObject json = new JSONObject(s);  
  147.                     Log.i(”onResponse: ”,json.toString());  
  148.                 } catch(JSONException e) {  
  149.                     e.printStackTrace();  
  150.                 }  
  151.             }  
  152.         },new Response.ErrorListener(){  
  153.             @Override  
  154.             public void onErrorResponse(VolleyError volleyError){  
  155.                 Log.i(”onResponse: ”,volleyError.toString());  
  156.             }  
  157.         });  
  158.         requestQueue.add(straingRequest);  
  159.     }  
  160.     public static JSONObject json = null;  
  161.     public static JSONObject getInfoFromUrl_Volley(String url,Context context)  
  162.     {  
  163.         json = null;  
  164.         RequestQueue requestQueue = InternetUtil.getmRequestqueue(context);  
  165.         StringRequest straingRequest = new StringRequest(url,new Response.Listener<String>(){  
  166.             @Override  
  167.             public void onResponse(String s){  
  168.                 try {  
  169.                     json = new JSONObject(s);  
  170.                     Log.i(”onResponse: ”,json.toString());  
  171.                 } catch(JSONException e) {  
  172.                     e.printStackTrace();  
  173.                 }  
  174.             }  
  175.         },new Response.ErrorListener(){  
  176.             @Override  
  177.             public void onErrorResponse(VolleyError volleyError){  
  178.                 Log.i(”onResponse: ”,volleyError.toString());  
  179.             }  
  180.         });  
  181.         requestQueue.add(straingRequest);  
  182.         return json;  
  183.     }  
  184.     public class UrlConstants {  
  185.         /** 
  186.          * 云音乐搜索API网址 
  187.          */  
  188.         public static final String CLOUD_MUSIC_API_SEARCH = “http://s.music.163.com/search/get/?”;  
  189.         /** 
  190.          * 歌曲信息API网址 
  191.          */  
  192.         public static final String CLOUD_MUSIC_API_MUSICINGO = “http://music.163.com/api/song/detail/?”;  
  193.         /** 
  194.          * 获取歌曲的歌词 
  195.          */  
  196.         public static final String CLOUD_MUSIC_API_MUSICLRC = “http://music.163.com/api/song/lyric?”;  
  197.         /** 
  198.          * 获取歌单 
  199.          */  
  200.         public static final String CLOUD_MUSIC_API_MUSICLIST = “http://music.163.com/api/playlist/detail?”;  
  201.     }  
  202. }  
package winter.zxb.smilesb101.testdemo;

import android.content.Context;
import android.util.Log;
import android.util.StateSet;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * 音乐网络类
 * Created by SmileSB101 on 2016/11/1 0001.
 */

public class MusicNetWork{
    /**
     * 网易音乐搜索API
     * http://s.music.163.com/search/get/
     * 获取方式:GET
     * 参数:
     * src: lofter //可为空
     * type: 1
     * filterDj: true|false //可为空
     * s: //关键词
     * limit: 10 //限制返回结果数
     * offset: 0 //偏移
     * callback: //为空时返回json,反之返回jsonp callback
     * @param s
     * @param context
     * @return
     * 注意废数字才用‘’符号,要不不能用,否则出错!!
     */
    public static void SearchMusic(Context context,String s,int limit,int type,int offset){
        String url = UrlConstants.CLOUD_MUSIC_API_SEARCH + "type="+type+"&s='" + s + "'&limit="+limit+"&offset="+offset;
        RequestQueue requestQueue = InternetUtil.getmRequestqueue(context);
        StringRequest straingRequest = new StringRequest(url,new Response.Listener<String>(){
            @Override
            public void onResponse(String s){
                try {
                    JSONObject json = new JSONObject(s);
                    Log.i("onResponse: ",json.toString());
                } catch(JSONException e) {
                    e.printStackTrace();
                }
            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError){
                Log.i("onResponse: ",volleyError.toString());
            }
        });
        requestQueue.add(straingRequest);
    }

    /**
     * 网易云音乐歌曲信息API
     * @param context
     * @param id 歌曲id
     * @param ids 用[]包裹起来的歌曲id 写法%5B %5D
     * @return
     */
    public static void Cloud_Music_MusicInfoAPI(Context context,String id,String ids)
    {
        String url = UrlConstants.CLOUD_MUSIC_API_MUSICINGO + "id="+id+"&ids=%5B"+ids+"%5D";
        RequestQueue requestQueue = InternetUtil.getmRequestqueue(context);
        StringRequest straingRequest = new StringRequest(url,new Response.Listener<String>(){
            @Override
            public void onResponse(String s){
                try {
                    JSONObject json = new JSONObject(s);
                    Log.i("onResponse: ",json.toString());
                } catch(JSONException e) {
                    e.printStackTrace();
                }
            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError){
                Log.i("onResponse: ",volleyError.toString());
            }
        });
        requestQueue.add(straingRequest);
    }

    /**
     * 获取歌曲歌词的API
     *URL:

     GET http://music.163.com/api/song/lyric

     必要参数:

     id:歌曲ID

     lv:值为-1,我猜测应该是判断是否搜索lyric格式

     kv:值为-1,这个值貌似并不影响结果,意义不明

     tv:值为-1,是否搜索tlyric格式
     * @param context
     * @param os
     * @param id
     */
    public static void Cloud_Muisc_getLrcAPI(Context context,String os,String id)
    {
        String url = UrlConstants.CLOUD_MUSIC_API_MUSICLRC + "os="+os+"&id="+id+"&lv=-1&kv=-1&tv=-1";
        RequestQueue requestQueue = InternetUtil.getmRequestqueue(context);
        StringRequest straingRequest = new StringRequest(url,new Response.Listener<String>(){
            @Override
            public void onResponse(String s){
                try {
                    JSONObject json = new JSONObject(s);
                    Log.i("onResponse: ",json.toString());
                } catch(JSONException e) {
                    e.printStackTrace();
                }
            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError){
                Log.i("onResponse: ",volleyError.toString());
            }
        });
        requestQueue.add(straingRequest);
    }

    /**
     * 获取歌单的API
     * @param context
     * @param id 歌单ID
     */
    public static void Cloud_Muisc_MusicListSearch(Context context,String id)
    {
        String url = UrlConstants.CLOUD_MUSIC_API_MUSICLIST + "id="+id+"&updateTime=-1";
        RequestQueue requestQueue = InternetUtil.getmRequestqueue(context);
        StringRequest straingRequest = new StringRequest(url,new Response.Listener<String>(){
            @Override
            public void onResponse(String s){
                try {
                    JSONObject json = new JSONObject(s);
                    Log.i("onResponse: ",json.toString());
                } catch(JSONException e) {
                    e.printStackTrace();
                }
            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError){
                Log.i("onResponse: ",volleyError.toString());
            }
        });
        requestQueue.add(straingRequest);
    }
    public static JSONObject json = null;
    public static JSONObject getInfoFromUrl_Volley(String url,Context context)
    {
        json = null;
        RequestQueue requestQueue = InternetUtil.getmRequestqueue(context);
        StringRequest straingRequest = new StringRequest(url,new Response.Listener<String>(){
            @Override
            public void onResponse(String s){
                try {
                    json = new JSONObject(s);
                    Log.i("onResponse: ",json.toString());
                } catch(JSONException e) {
                    e.printStackTrace();
                }
            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError){
                Log.i("onResponse: ",volleyError.toString());
            }
        });
        requestQueue.add(straingRequest);
        return json;
    }
    public class UrlConstants {
        /**
         * 云音乐搜索API网址
         */
        public static final String CLOUD_MUSIC_API_SEARCH = "http://s.music.163.com/search/get/?";
        /**
         * 歌曲信息API网址
         */
        public static final String CLOUD_MUSIC_API_MUSICINGO = "http://music.163.com/api/song/detail/?";
        /**
         * 获取歌曲的歌词
         */
        public static final String CLOUD_MUSIC_API_MUSICLRC = "http://music.163.com/api/song/lyric?";
        /**
         * 获取歌单
         */
        public static final String CLOUD_MUSIC_API_MUSICLIST = "http://music.163.com/api/playlist/detail?";
    }
}

调用类:

  1. package winter.zxb.smilesb101.testdemo;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.   
  9. public class MainActivity extends AppCompatActivity implements View.OnClickListener{  
  10.   
  11.     private Button NetWorkTest;  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState){  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         NetWorkTest = (Button)findViewById(R.id.NetWorkTest);  
  17.         NetWorkTest.setOnClickListener(this);  
  18.     }  
  19.   
  20.     @Override  
  21.     public void onClick(View v){  
  22.         switch(v.getId())  
  23.         {  
  24.             case R.id.NetWorkTest://网络测试  
  25.                 Log.i(”进入”,“onClick: ”);  
  26.                 //MusicNetWork.SearchMusic(this,”光荣”,10,1,0);  
  27.                 //MusicNetWork.Cloud_Music_MusicInfoAPI(this,”26562231”,”26562231”);  
  28.                 //MusicNetWork.Cloud_Muisc_getLrcAPI(this,”pc”,”26562231”);  
  29.                 MusicNetWork.Cloud_Muisc_MusicListSearch(this,“37880978”);  
  30.                 break;  
  31.         }  
  32.     }  
  33. }  
package winter.zxb.smilesb101.testdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button NetWorkTest;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NetWorkTest = (Button)findViewById(R.id.NetWorkTest);
        NetWorkTest.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch(v.getId())
        {
            case R.id.NetWorkTest://网络测试
                Log.i("进入","onClick: ");
                //MusicNetWork.SearchMusic(this,"光荣",10,1,0);
                //MusicNetWork.Cloud_Music_MusicInfoAPI(this,"26562231","26562231");
                //MusicNetWork.Cloud_Muisc_getLrcAPI(this,"pc","26562231");
                MusicNetWork.Cloud_Muisc_MusicListSearch(this,"37880978");
                break;
        }
    }
}


源码我已经上传到CSDN(但是源码相对于没有那么全,但是道理是一样的):源码地址

转自张孝波的博客:http://blog.csdn.net/qq_21265915/article/details/53000005

展开阅读全文

没有更多推荐了,返回首页