java获取网易云音乐的搜索结果

网易云音乐的接口:http://music.163.com/api/search/pc
参数 查询内容:s

		String jsonStr = "千之缘";
		//定义查询的内容
        try {
            String str = URLEncoder.encode(jsonStr, "utf-8").replaceAll("\\+", "");
            // 转换成encode
            URL url = new URL("http://music.163.com/api/search/pc?s=" + str + "&type=1");
			//拼接url
            HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
            httpCon.setConnectTimeout(3000);
            httpCon.setDoInput(true);
            httpCon.setRequestMethod("GET");
            // 获取相应码
            int respCode = httpCon.getResponseCode();
            if (respCode == 200) {
                // ByteArrayOutputStream相当于内存输出流
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                // 将输入流转移到内存输出流中
                while ((len = httpCon.getInputStream().read(buffer, 0, buffer.length)) != -1) 
                    out.write(buffer, 0, len);
                }
                // 将内存流转换为字符串
                jsonStr = new String(out.toByteArray());
            } else {
               System.out.println("网易云错误,错误码为"+respCode );
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(JSONObject.parseObject(jsonStr).getString("result")).getString("songs"));
        //jsonArray 内就是歌曲信息了 之后可以用jsonArray.getJSONObject(0).getString(/*这里放你要获取的字段名如id*/)获取详细信息
   
       

你可能感兴趣的:(java获取网易云音乐的搜索结果)