java读取最新网易云音乐API

2021/11/2 17:12:28

本文主要是介绍java读取最新网易云音乐API,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

最新网易云音乐API 亲测可用 JAVA代码只是示例其他语言也适用

1.maven引入

<!-- jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.12.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>
 
 
 
        <dependency>  
         <groupId>org.apache.httpcomponents</groupId>  
          <artifactId>httpclient-cache</artifactId>  
         <version>4.5.6</version>          
        </dependency>  
        <dependency>  
         <groupId>org.apache.httpcomponents</groupId>  
          <artifactId>httpmime</artifactId>  
         <version>4.5.6</version>          
        </dependency>
 

2.API处理代码,通过关键词搜索歌曲,并且获取歌曲名称、图片、歌手、真实播放地址等信息。

//网易云音乐
				public static  MusicShare  musicApi(String name) {
					
					
					MusicShare musicShare  = new MusicShare ();
					
					String url ="https://api.imjad.cn/cloudmusic/?type=search&search_type=1&s="+name;
					
					 
			
					try {
						CloseableHttpClient httpclient = HttpClients.createDefault();
						
						HttpGet get = new HttpGet(url);
						
						
						RequestConfig requestConfig = RequestConfig.custom()  
						        .setConnectTimeout(60000).setConnectionRequestTimeout(60000)  
						        .setSocketTimeout(60000).build();  //设置超时
						get.setConfig(requestConfig);
						
						
					
						
						HttpResponse response = httpclient.execute(get);
						int statusCode = response.getStatusLine().getStatusCode();
						
						   System.out.println(statusCode);
						
						//if(statusCode==200){
						    //获取响应结果
						   String result = EntityUtils.toString(response.getEntity());
						   
						   
						   

						  
						    
						    System.out.println(result);
						    
						    ObjectMapper mapper = new ObjectMapper();
						    
						    JsonNode root = mapper.readTree(result);
						    
						    Iterator<JsonNode> it = root.get("result").get("songs").iterator();
						    
						  
						    
						    List<MusicShare> musicShares = new ArrayList<MusicShare>();
						    
						    while (it.hasNext()) {
						    	MusicShare mu = new MusicShare();
						    	
					        	JsonNode node = it.next();
					        	
					        	String id = node.get("id").asText();
					        	String title = node.get("name").asText();
					        	
					        	String pictureUrl = node.get("al").get("picUrl").asText();
					        	String jumpUrl = "https://y.music.163.com/m/song?id="+id+"&app_version=8";
					        	
					        	
					        	String summary = "";
					        	
					        	 Iterator<JsonNode> summaryIt = node.get("ar").iterator();
					        	 while (summaryIt.hasNext()) {
					        		 JsonNode de = summaryIt.next();
					        		 summary = de.get("name").asText();
					        		 
					        		 
					        	 }
					        	
					        	
					        	
					        	 mu.setId(id);
					        	 mu.setTitle(title);
					        	 mu.setPictureUrl(pictureUrl);
					        	 mu.setSummary(summary);
					        	 mu.setJumpUrl(jumpUrl);
					        	
					        	musicShares.add(mu);
					        	
					        	
						    
						    }
						    
						    int i = musicShares.size();
						    int random=(int)(Math.random()*i+0);
						    
						    musicShare = musicShares.get(random);
						    
						    
						   
						   
						    
						
						   if(StringUtils.isEmpty(musicShare.getId())) {
							   return musicShare;
						   }
						   
						   
						   
							 url ="https://api.imjad.cn/cloudmusic/?type=song&id="+musicShare.getId();
						   
							 
							 get = new HttpGet(url);
								
								
								 requestConfig = RequestConfig.custom()  
								        .setConnectTimeout(60000).setConnectionRequestTimeout(60000)  
								        .setSocketTimeout(60000).build();  //设置超时
								get.setConfig(requestConfig);
								
								
							
								
							  response = httpclient.execute(get);
							  statusCode = response.getStatusLine().getStatusCode();
								
								   System.out.println(statusCode);
								
								//if(statusCode==200){
								    //获取响应结果
								    result = EntityUtils.toString(response.getEntity());
								   
								    root = mapper.readTree(result);
								    
								    it = root.get("data").iterator();
								    String musicUrl = "";
								    
								    while (it.hasNext()) {
								   
								    	
							        	JsonNode node = it.next();
							        	musicUrl = node.get("url").asText();
							        	
							        	
								    }
								    
								    
								    
					      
						
						   
						    musicShare.setMusicUrl(musicUrl);
						    
						    
					     
						    
							   System.out.println(musicShare.getId()+""+musicShare.getTitle()+""+musicShare.getSummary()+" "+musicShare.getPictureUrl()+" "+musicShare.getMusicUrl());
							   
							   	    
						    
						    
						    
					} catch (Exception e) {
						// TODO Auto-generated catch block
					
					}
					return musicShare;
					
					
					
					
					
					
				}



这篇关于java读取最新网易云音乐API的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程