diff --git a/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java b/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java index 6f9358c..2e4af99 100644 --- a/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java +++ b/src/main/java/com/yutou/bilibili/BiliBili/LiveUtils.java @@ -25,7 +25,7 @@ import java.util.zip.Inflater; public class LiveUtils { private static String cookie = null; - private static Map liveStateMap=new HashMap<>(); + private static Map liveStateMap = new HashMap<>(); public LiveUtils() { @@ -53,7 +53,8 @@ public class LiveUtils { } } } - public static String getHex(byte[] bytes){ + + public static String getHex(byte[] bytes) { return DatatypeConverter.printHexBinary(bytes); } @@ -186,7 +187,7 @@ public class LiveUtils { public synchronized static JSONObject http_get(String url) { try { - // Log.i("调用url = "+url); + // Log.i("调用url = "+url); HttpURLConnection connection = getBiliHttpGet(url, getCookie()); BufferedInputStream stream = new BufferedInputStream(connection.getInputStream()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -348,21 +349,27 @@ public class LiveUtils { } public static JSONObject getLiveInfo(int roomId) { - return http_get("https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=" + roomId+"&protocol=0,1&format=0,1,2&codec=0,1&qn=10000&platform=web&ptype=8"); + return http_get("https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=" + roomId + "&protocol=0,1&format=0,1,2&codec=0,1&qn=10000&platform=web&ptype=8"); } - public static boolean getLiveState(int roomId){ + + public static boolean getLiveState(int roomId) { return liveStateMap.get(roomId); } - public static void reloadLiveState(int roomId){ + + public static void reloadLiveState(int roomId) { JSONObject json = getLiveInfo(roomId); - if(json!=null){ - liveStateMap.put(roomId,json.getJSONObject("data").getInteger("live_status") == 1); + if (json != null) { + try { + liveStateMap.put(roomId, json.getJSONObject("data").getInteger("live_status") == 1); + } catch (Exception e) { + System.out.println("json = " + json); + } } } public static String getLiveTitle(int roomId) { - JSONObject json =getLiveInfo(roomId); + JSONObject json = getLiveInfo(roomId); if (json != null) { return json.getJSONObject("data").getJSONObject("room_info").getString("title"); } @@ -375,10 +382,10 @@ public class LiveUtils { } reloadLiveState(roomId); return false;*/ - if(!Live.lives.containsKey(roomId)){ + if (!Live.lives.containsKey(roomId)) { return false; } - return Live.lives.get(roomId).geData().getLive()==1; + return Live.lives.get(roomId).geData().getLive() == 1; } private static void checkLiveSave(int roomId) { @@ -413,13 +420,13 @@ public class LiveUtils { public void init() { JSONObject json = http_get("https://api.live.bilibili.com/xlive/web-room/v1/giftPanel/giftConfig?platform=pc"); - if(json==null){ + if (json == null) { return; } JSONArray list = json.getJSONObject("data").getJSONArray("list"); for (Object o : list) { JSONObject item = (JSONObject) o; - RedisTools.set(2,"bili_gift_"+item.getInteger("id"),item.toJSONString()); + RedisTools.set(2, "bili_gift_" + item.getInteger("id"), item.toJSONString()); } }