List of usage examples for com.google.gson JsonObject getAsJsonArray
public JsonArray getAsJsonArray()
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 a v a 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.geecko.QuickLyric.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 a v a2 s. co 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 (JsonParseException | IndexOutOfBoundsException e) { return new Lyrics(NO_RESULT); } catch (Exception e) { return new Lyrics(ERROR); } Lyrics lyrics = new Lyrics(POSITIVE_RESULT); lyrics.setArtist(artist); lyrics.setTitle(title); lyrics.setText(text); lyrics.setSource(domain); lyrics.setURL(url); return lyrics; }
From source file:com.geecko.QuickLyric.MetalArchives.java
License:Open Source License
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 text;/*from www. j av a 2 s . c o m*/ 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()); 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 (JsonParseException | IndexOutOfBoundsException e) { return new Lyrics(NO_RESULT); } catch (Exception e) { return new Lyrics(ERROR); } Lyrics lyrics = new Lyrics(POSITIVE_RESULT); lyrics.setText(text); return lyrics; }
From source file:com.liferay.mobile.sdk.json.ActionDeserializer.java
License:Open Source License
@Override public Action deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject root = json.getAsJsonObject(); String path = root.get("path").getAsString(); String response = root.getAsJsonObject("returns").get("type").getAsString(); String serviceClassName = null; String methodName = null;/* www . ja v a 2 s . c o m*/ String name = root.get("name").getAsString(); String[] values = name.split("#"); if (values.length == 2) { serviceClassName = values[0]; methodName = values[1]; } JsonArray jsonArray = root.getAsJsonArray("parameters").getAsJsonArray(); try { ArrayList<Parameter> parameters = JSONParser.fromJSON(jsonArray, new GenericListType<>(Parameter.class)); return new Action(serviceClassName, methodName, path, response, parameters); } catch (Exception e) { throw new JsonParseException(e); } }
From source file:com.simonmacdonald.muzei.comic.ComicCoverArtSource.java
License:Open Source License
private int getRandomVolumeID(String jsonFile) { try {// w w w. j a va2s. co m BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open(jsonFile), "UTF-8")); Gson gson = new Gson(); JsonObject obj = gson.fromJson(reader, JsonObject.class); JsonArray array = obj.getAsJsonArray("volumes").getAsJsonArray(); Random random = new Random(); int id = array.get(random.nextInt(array.size())).getAsJsonObject().get("id").getAsInt(); // Log.d(TAG, "id is = " + id); return id; } catch (Exception e) { // return Batmans return 796; } }
From source file:com.simonmacdonald.muzei.comic.ComicCoverArtSource.java
License:Open Source License
private Comic fetchComic(int volumeId, int firstIssue, int lastIssue) { Random random = new Random(); int issueId = random.nextInt(lastIssue); if (issueId == 0) { issueId = 1;//from ww w . ja v a 2s . co m } String strUrl = "http://www.comicvine.com/api/issues/?api_key=" + API.COMICVINE_KEY + "&format=json&" + "filter=issue_number:" + issueId + ",volume:" + volumeId; JsonObject obj = makeComicVineApiRequest(strUrl); JsonObject volume2 = obj.getAsJsonArray("results").getAsJsonArray().get(0).getAsJsonObject() .getAsJsonObject("volume").getAsJsonObject(); JsonObject image = obj.getAsJsonArray("results").getAsJsonArray().get(0).getAsJsonObject() .getAsJsonObject("image").getAsJsonObject(); String title = volume2.get("name").getAsString() + " #" + obj.getAsJsonArray("results").getAsJsonArray() .get(0).getAsJsonObject().get("issue_number").getAsString(); String imageUrl = image.get("super_url").getAsString(); String detailsUrl = volume2.get("site_detail_url").getAsString(); return new Comic(title, imageUrl, detailsUrl); }