赞
踩
首先将以下需要的包导入
gson-2.8.0.jar
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-lang-2.4.jar
commons-logging.jar
ezmorph-1.0.6.jar
附上下载地址链接:https://pan.baidu.com/s/1z5gOjYrzNNXJNYdMosI0yA 提取码:6732
贴上代码
- package music;
-
- import com.google.gson.JsonArray;
- /**
- * Created by david on 2017-7-5.
- */
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
-
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.List;
- import net.sf.json.JsonConfig;
- public class paihang {
- /**
- * 发起http请求并获取结果
- * @param requestUrl 请求地址
- */
- public static JSONObject getXpath(String requestUrl){
- String res="";
- JSONObject object = null;
- StringBuffer buffer = new StringBuffer();
- try{
- URL url = new URL(requestUrl);
- HttpURLConnection urlCon= (HttpURLConnection)url.openConnection();
- if(200==urlCon.getResponseCode()){
- InputStream is = urlCon.getInputStream();
- InputStreamReader isr = new InputStreamReader(is,"utf-8");
- BufferedReader br = new BufferedReader(isr);
-
- String str = null;
- while((str = br.readLine())!=null){
- buffer.append(str);
- }
- br.close();
- isr.close();
- is.close();
- res = buffer.toString();
- //JsonParser parse =new JsonParser();
- object = JSONObject.fromObject(res);
- return object;
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- //return object;
- return null;
- }
-
- public static JsonObject postDownloadJson(String path,String post){
- URL url = null;
- try {
- url = new URL(path);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");// 提交模式
- // conn.setConnectTimeout(10000);//连接超时 单位毫秒
- // conn.setReadTimeout(2000);//读取超时 单位毫秒
- // 发送POST请求必须设置如下两行
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- // 获取URLConnection对象对应的输出流
- PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
- // 发送请求参数
- printWriter.write(post);//post的参数 xx=xx&yy=yy
- // flush输出流的缓冲
- printWriter.flush();
- //开始获取数据
- BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- int len;
- byte[] arr = new byte[1024];
- while((len=bis.read(arr))!= -1){
- bos.write(arr,0,len);
- bos.flush();
- }
- bos.close();
- JsonParser parse = new JsonParser();
- return (JsonObject)parse.parse(bos.toString("utf-8"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
- //测试
- public static void main(String args [] ) {
- System.out.println("开始啦");
- //获取返回json对象
- JSONObject object = getXpath("https://api.bzqll.com/music/netease/songList?key=579621905&id=3778678&limit=10&offset=0");
- /**
- * {"result":"SUCCESS",
- * "code":200,
- * "data":
- * {
- * "songListId":"3778678",
- * "songListName":"云音乐热歌榜",
- * "songListPic":"https://p2.music.126.net/GhhuF6Ep5Tq9IEvLsyCN7w==/18708190348409091.jpg?param=300y300","songListCount":200,"songListPlayCount":2880181248,"songListDescription":"云音乐热歌榜:云音乐用户一周内收听所有线上歌曲 官方TOP排行榜,每周四更新。","songListUserId":1,
- * "songs":
- * [
- * {"id":"574566207",
- * "name":"盗将行","
- * singer":"花粥/马雨阳",
- * "pic":"https://api.bzqll.com/music/netease/pic?id=574566207&imgSize=300&key=579621905",
- * "lrc":"https://api.bzqll.com/music/netease/lrc?id=574566207&key=579621905",
- * "url":"https://api.bzqll.com/music/netease/url?id=574566207&key=579621905",
- * "time":198},
- * {"id":"574566207",
- * "name":"盗将行","
- * singer":"花粥/马雨阳",
- * "pic":"https://api.bzqll.com/music/netease/pic?id=574566207&imgSize=300&key=579621905",
- * "lrc":"https://api.bzqll.com/music/netease/lrc?id=574566207&key=579621905",
- * "url":"https://api.bzqll.com/music/netease/url?id=574566207&key=579621905",
- * "time":198}
- *
- * ]
- **/
- JSONArray array = object.getJSONObject("data").getJSONArray("songs");//获取json数组
- /**
- * 第一个参数为需转的json数组,第二个参数为对应实体类,
- */
- List<music> list = (List)JSONArray.toList(array, new music(), new JsonConfig());
- music m = list.get(20);
- System.out.println(object);
- System.out.println(m.getLrc());
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。