public static boolean isLegalJson(String json) { if ((json).isEmpty()) { return false; } try { new JsonParser().parse(json); return true; } catch (JsonParseException e) { e.printStackTrace(); return false; } } }
private static String getNewestVersion() { try { URL url = new URL(URL + PLUGIN + LATEST_VERSION + "?" + System.currentTimeMillis()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setUseCaches(true); connection.addRequestProperty("User-Agent", "ViaVersion " + Via.getPlatform().getPluginVersion() + " " + Via.getPlatform().getPlatformName()); connection.setDoOutput(true); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String input; String content = ""; while ((input = br.readLine()) != null) { content = content + input; } br.close(); JsonObject statistics; try { statistics = GsonUtil.getGson().fromJson(content, JsonObject.class); } catch (JsonParseException e) { e.printStackTrace(); return null; } return statistics.get("name").getAsString(); } catch (MalformedURLException e) { return null; } catch (IOException e) { return null; } } }
/** * Get Subtitle for the specific trackID. * * @param trackID * @return * @throws MusixMatchException */ public Subtitle getSubtitle(int trackID) throws MusixMatchException { Subtitle subtitle = null; SubtitleGetMessage message = null; Map<String, Object> params = new HashMap<String, Object>(); params.put(Constants.API_KEY, apiKey); params.put(Constants.TRACK_ID, new String("" + trackID)); String response = null; response = MusixMatchRequest.sendRequest(Helper.getURLString( Methods.TRACK_SUBTITLE_GET, params)); Gson gson = new Gson(); try { message = gson.fromJson(response, SubtitleGetMessage.class); } catch (JsonParseException jpe) { jpe.printStackTrace(); handleErrorResponse(response); } subtitle = message.getContainer().getBody().getSubtitle(); return subtitle; }