安卓 网易云音乐能用的API

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_21265915/article/details/53000005

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

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

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

实现

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

Gson.jar下载地址:Gson.jar

使用方法:

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

下面附上工具类代码:

1.获取RequestQueue实例:

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.网络请求处理类

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?";
	}
}

调用类:

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(但是源码相对于没有那么全,但是道理是一样的):源码地址

展开阅读全文

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