List of usage examples for com.google.gson JsonParser JsonParser
@Deprecated
public JsonParser()
From source file:com.evandroid.musica.lyrics.MetalArchives.java
License:Open Source License
@Reflection public static Lyrics fromMetaData(String artist, String title) { String baseURL = "http://www.metal-archives.com/search/ajax-advanced/searching/songs/?bandName=%s&songTitle=%s&releaseType[]=1&exactSongMatch=1&exactBandMatch=1"; String urlArtist = artist.replaceAll("\\s", "+"); String urlTitle = title.replaceAll("\\s", "+"); String url;//from ww w. j ava 2 s . c o m String text; try { String response = Net.getUrlAsString(String.format(baseURL, urlArtist, urlTitle)); JsonObject jsonResponse = new JsonParser().parse(response).getAsJsonObject(); JsonArray track = jsonResponse.getAsJsonArray("aaData").get(0).getAsJsonArray(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < track.size(); i++) builder.append(track.get(i).getAsString()); Document trackDocument = Jsoup.parse(builder.toString()); url = trackDocument.getElementsByTag("a").get(1).attr("href"); String id = trackDocument.getElementsByClass("viewLyrics").get(0).id().substring(11); text = Jsoup.connect("http://www.metal-archives.com/release/ajax-view-lyrics/id/" + id).get().body() .html(); } catch (IOException e) { return new Lyrics(Lyrics.ERROR); } catch (JsonParseException e) { return new Lyrics(Lyrics.NO_RESULT); } Lyrics lyrics = new Lyrics(Lyrics.POSITIVE_RESULT); lyrics.setArtist(artist); lyrics.setTitle(title); lyrics.setText(text); lyrics.setSource(domain); lyrics.setURL(url); return lyrics; }
From source file:com.evandroid.musica.tasks.IdDecoder.java
License:Open Source License
@Override protected Lyrics doInBackground(String... strings) { String url = strings[0];/*from w w w .j a va 2 s . c o m*/ String artist; String track; if (url == null) return new Lyrics(Lyrics.ERROR); if (url.contains("//www.soundhound.com/")) { try { // todo switch to Jsoup String html = Net.getUrlAsString(url); int preceding = html.indexOf("root.App.trackDa") + 19; int following = html.substring(preceding).indexOf(";"); String data = html.substring(preceding, preceding + following); JsonObject jsonData = new JsonParser().parse(data).getAsJsonObject(); artist = jsonData.get("artist_display_name").getAsString(); track = jsonData.get("track_name").getAsString(); } catch (IOException e) { e.printStackTrace(); return new Lyrics(Lyrics.ERROR); } } else if (url.contains("//shz.am/")) { try { Document doc = Jsoup.connect(url.trim()).get(); track = doc.getElementsByAttribute("data-track-title").text(); artist = doc.getElementsByAttribute("data-track-artist").text(); } catch (IOException e) { e.printStackTrace(); return new Lyrics(Lyrics.ERROR); } } else if (url.contains("//play.google.com/store/music/")) { String docID = url.substring(url.indexOf("&tid=") + 5); try { Document doc = Jsoup.connect(url).get(); Element playCell = doc.getElementsByAttributeValue("data-track-docid", docID).get(0); artist = doc.getElementsByClass("primary").text(); track = playCell.parent().parent().child(1).getElementsByClass("title").text(); } catch (IOException e) { e.printStackTrace(); return new Lyrics(Lyrics.ERROR); } } else return new Lyrics(Lyrics.ERROR); Lyrics res = new Lyrics(Lyrics.SEARCH_ITEM); res.setArtist(artist); res.setTitle(track); return res; }
From source file:com.exalttech.trex.util.Util.java
License:Apache License
/** * Convert JSON to pretty format// w w w . j ava 2 s . c o m * * @param jsonString * @return */ public static String toPrettyFormat(String jsonString) { try { JsonParser parser = new JsonParser(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); // Check if it is an Array if ('[' == jsonString.charAt(0)) { JsonArray jsonArray = parser.parse(jsonString).getAsJsonArray(); return getSubString(gson.toJson(jsonArray)); } else { JsonObject json = parser.parse(jsonString).getAsJsonObject(); return getSubString(gson.toJson(json)); } } catch (JsonSyntaxException ex) { // return the original string in case of exception LOG.error("Error formatting string", ex); return getSubString(jsonString); } }
From source file:com.example.counter.LoadSave.java
License:GNU General Public License
public List<CounterModel> loadFromFile(String FILENAME2, List<CounterModel> objList, Context appcontext) { try {/*from w w w . j a v a 2 s . c om*/ FileInputStream fis = appcontext.openFileInput(FILENAME2); //http://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonArray jArray = parser.parse(new InputStreamReader(fis)).getAsJsonArray(); for (JsonElement obj : jArray) { CounterModel cam = gson.fromJson(obj, CounterModel.class); objList.add(cam); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return objList; }
From source file:com.example.mediastock.activities.MusicPlayerActivity.java
/** * Thread to add the info of the music to database * * @param path the path where the music is stored * @param musicID the id of the music//from w w w . j a v a 2 s.c o m * @param title the title of the music */ private void addMusicInfoToDB(final String path, final int musicID, final String title) { new ExecuteExecutor(this, 2, new ExecuteExecutor.CallableAsyncTask(this) { @Override public String call() { MusicPlayerActivity context = (MusicPlayerActivity) getContextRef(); HttpURLConnection con = null; InputStream is = null; String genre = ""; // first, get the genre of the music try { URL url = new URL("https://api.shutterstock.com/v2/audio/" + context.musicID); con = (HttpURLConnection) url.openConnection(); con.setRequestProperty("Authorization", "Basic " + Utilities.getLicenseKey()); con.setConnectTimeout(25000); con.setReadTimeout(25000); if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { is = con.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = Utilities.readAll(rd); rd.close(); JsonElement json = new JsonParser().parse(jsonText); JsonObject o = json.getAsJsonObject(); JsonArray array = o.get("genres") != null ? o.get("genres").getAsJsonArray() : null; if (array != null) { if (array.size() > 0) genre += array.get(0) != null ? array.get(0).getAsString() : " - "; } // save music info to database context.db.insertMusicInfo(path, musicID, title, genre); } } catch (SocketTimeoutException e) { if (con != null) con.disconnect(); } catch (IOException e) { e.printStackTrace(); } finally { // and finally we close the objects try { if (is != null) { con.disconnect(); is.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } }); }
From source file:com.example.mediastock.activities.VideoPlayerActivity.java
/** * Thread to add to database the videos info * * @param path the path where the video is stored * @param videoID the id of the video * @param description the description of the video *//*from w w w. j a va 2 s .c o m*/ private void addVideoInfoToDB(final String path, final int videoID, final String description) { new ExecuteExecutor(this, 2, new ExecuteExecutor.CallableAsyncTask(this) { @Override public String call() { VideoPlayerActivity context = (VideoPlayerActivity) getContextRef(); HttpURLConnection con = null; InputStream is = null; String category = ""; // first, get the category of the video try { URL url = new URL("https://api.shutterstock.com/v2/videos/" + context.videoID); con = (HttpURLConnection) url.openConnection(); con.setRequestProperty("Authorization", "Basic " + Utilities.getLicenseKey()); con.setConnectTimeout(25000); con.setReadTimeout(25000); if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { is = con.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = Utilities.readAll(rd); rd.close(); JsonElement json = new JsonParser().parse(jsonText); JsonObject o = json.getAsJsonObject(); JsonArray array = o.get("categories") != null ? o.get("categories").getAsJsonArray() : null; if (array != null) { if (array.size() > 0) category += array.get(0).getAsJsonObject().get("name") != null ? array.get(0).getAsJsonObject().get("name").getAsString() : " - "; } // save video info to database context.db.insertVideoInfo(path, videoID, description, category); } } catch (SocketTimeoutException e) { if (con != null) con.disconnect(); } catch (IOException e) { e.printStackTrace(); } finally { // clean the objects try { if (is != null) { con.disconnect(); is.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } }); }
From source file:com.example.mediastock.util.DownloadService.java
/** * Method which downloads the images and the info. * * @param urlStr the URL//from ww w. j a v a2s .c o m * @param receiver the result receiver */ private void getImages(String urlStr, ResultReceiver receiver) { InputStream is = null; HttpURLConnection con = null; try { URL url = new URL(urlStr); con = (HttpURLConnection) url.openConnection(); con.setRequestProperty("Authorization", "Basic " + Utilities.getLicenseKey()); con.setConnectTimeout(25000); con.setReadTimeout(25000); if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { is = con.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = Utilities.readAll(rd); rd.close(); JsonElement json = new JsonParser().parse(jsonText); JsonObject o = json.getAsJsonObject(); JsonArray array = o.get("data").getAsJsonArray(); if (array.size() == 0) { publishImageResult(null, 3, receiver); con.disconnect(); is.close(); return; } int i = 0; for (JsonElement element : array) { JsonObject jsonObj = element.getAsJsonObject(); JsonObject assets = jsonObj.get("assets") == null ? null : jsonObj.get("assets").getAsJsonObject(); final ImageBean bean = new ImageBean(); if (assets != null) { bean.setId(jsonObj.get("id") == null ? null : jsonObj.get("id").getAsInt()); bean.setDescription(jsonObj.get("description") == null ? null : jsonObj.get("description").getAsString()); bean.setIdContributor(jsonObj.get("contributor") == null ? null : jsonObj.get("contributor").getAsJsonObject().get("id").getAsInt()); bean.setUrl(assets.get("preview") == null ? null : assets.get("preview").getAsJsonObject().get("url").getAsString()); } bean.setPos(i); i++; // update UI publishImageResult(bean, 1, receiver); } } } catch (SocketTimeoutException e) { if (con != null) con.disconnect(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); if (con != null) con.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.example.mediastock.util.DownloadService.java
/** * Method which downloads the music from the server. * * @param urlStr the URL/*from www . ja v a 2 s . co m*/ * @param receiver the result receiver */ private void getMusic(String urlStr, ResultReceiver receiver) { InputStream is = null; HttpURLConnection con = null; try { URL url = new URL(urlStr); con = (HttpURLConnection) url.openConnection(); con.setRequestProperty("Authorization", "Basic " + Utilities.getLicenseKey()); con.setConnectTimeout(25000); con.setReadTimeout(25000); if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { is = con.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = Utilities.readAll(rd); JsonElement json = new JsonParser().parse(jsonText); JsonObject o = json.getAsJsonObject(); JsonArray array = o.get("data").getAsJsonArray(); if (array.size() == 0) { publishMusicResult(null, 3, receiver); con.disconnect(); is.close(); return; } int i = 0; for (JsonElement element : array) { JsonObject jsonObj = element.getAsJsonObject(); JsonObject assets = jsonObj.get("assets").getAsJsonObject(); final MusicBean bean = new MusicBean(); if (assets != null) { bean.setId(jsonObj.get("id") == null ? null : jsonObj.get("id").getAsString()); bean.setTitle(jsonObj.get("title") == null ? null : jsonObj.get("title").getAsString()); bean.setPreview(assets.get("preview_mp3") == null ? null : assets.get("preview_mp3").getAsJsonObject().get("url").getAsString()); } bean.setPos(i); i++; // update the UI publishMusicResult(bean, 1, receiver); } } } catch (SocketTimeoutException e) { if (con != null) con.disconnect(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); if (con != null) con.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.exorath.simpleapi.impl.hub.discovery.serverlist.HubServerImpl.java
License:Apache License
public JsonObject serializeToJson() { JsonObject object = new JsonObject(); object.addProperty("bungee", getBungeeName()); object.addProperty("joinable", isJoinable()); object.addProperty("maxplayers", getPlayerAmount()); JsonArray playersArray = new JsonArray(); players.forEach(p -> playersArray.add(new JsonParser().parse(p.serialize()))); return object; }
From source file:com.exorath.simpleapi.impl.player.SerializedPlayerImpl.java
License:Apache License
public static SerializedPlayer deserialize(String message) { JsonObject obj = new JsonParser().parse(message).getAsJsonObject(); return new SerializedPlayerImpl(obj.get("name").getAsString(), UUID.fromString(obj.get("uuid").getAsString())); }