List of usage examples for com.google.gson.stream JsonReader endObject
public void endObject() throws IOException
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
private JsonReader prepareReader(Map<String, String> params, boolean firstCall) throws IOException { InputStream in = doRequest(params); if (in == null) return null; JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); // Check if content contains array or object, array indicates login-response or error, object is content try {//from w w w. j a va 2 s . com reader.beginObject(); } catch (Exception e) { e.printStackTrace(); return null; } while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("content")) { JsonToken t = reader.peek(); if (t.equals(JsonToken.BEGIN_ARRAY)) { return reader; } else if (t.equals(JsonToken.BEGIN_OBJECT)) { JsonObject object = new JsonObject(); reader.beginObject(); String nextName = reader.nextName(); // We have a BEGIN_OBJECT here but its just the response to call "subscribeToFeed" if ("status".equals(nextName)) return reader; // Handle error while (reader.hasNext()) { if (nextName != null) { object.addProperty(nextName, reader.nextString()); nextName = null; } else { object.addProperty(reader.nextName(), reader.nextString()); } } reader.endObject(); if (object.get(ERROR) != null) { String message = object.get(ERROR).toString(); if (message.contains(NOT_LOGGED_IN)) { lastError = NOT_LOGGED_IN; if (firstCall && login() && !hasLastError) return prepareReader(params, false); // Just do the same request again else return null; } if (message.contains(API_DISABLED)) { hasLastError = true; lastError = MyApplication.context().getString(R.string.Error_ApiDisabled, Controller.getInstance().username()); return null; } // Any other error hasLastError = true; lastError = message; } } } else { reader.skipValue(); } } return null; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
private Set<String> parseAttachments(JsonReader reader) throws IOException { Set<String> ret = new HashSet<>(); reader.beginArray();/*from ww w . j a v a 2 s . co m*/ while (reader.hasNext()) { String attId = null; String attUrl = null; reader.beginObject(); while (reader.hasNext()) { try { switch (reader.nextName()) { case CONTENT_URL: attUrl = reader.nextString(); break; case ID: attId = reader.nextString(); break; default: reader.skipValue(); break; } } catch (IllegalArgumentException e) { e.printStackTrace(); reader.skipValue(); } } reader.endObject(); if (attId != null && attUrl != null) ret.add(attUrl); } reader.endArray(); return ret; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
/** * parse articles from JSON-reader/* w w w. ja va2 s . com*/ * * @param articles container, where parsed articles will be stored * @param reader JSON-reader, containing articles (received from server) * @param skipNames set of names (article properties), which should not be processed (may be {@code null}) * @param filter filter for articles, defining which articles should be omitted while parsing (may be {@code * null}) * @return amount of processed articles */ private int parseArticleArray(final Set<Article> articles, JsonReader reader, Set<Article.ArticleField> skipNames, IArticleOmitter filter) { long time = System.currentTimeMillis(); int count = 0; try { reader.beginArray(); while (reader.hasNext()) { Article article = new Article(); boolean skipObject = false; reader.beginObject(); skipObject = parseArticle(article, reader, skipNames, filter); reader.endObject(); if (!skipObject && article.id != -1 && article.title != null) articles.add(article); count++; } reader.endArray(); } catch (OutOfMemoryError e) { Controller.getInstance().lowMemory(true); // Low memory detected } catch (Exception e) { Log.e(TAG, "Input data could not be read: " + e.getMessage() + " (" + e.getCause() + ")", e); } Log.d(TAG, String.format("parseArticleArray: parsing %s articles took %s ms", count, (System.currentTimeMillis() - time))); return count; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
/** * Retrieves all categories./*from www . j a v a2 s. c o m*/ * * @return a list of categories. */ public Set<Category> getCategories() { long time = System.currentTimeMillis(); Set<Category> ret = new LinkedHashSet<>(); if (sessionNotAlive()) return ret; Map<String, String> params = new HashMap<>(); params.put(PARAM_OP, VALUE_GET_CATEGORIES); JsonReader reader = null; try { reader = prepareReader(params); if (reader == null) return ret; reader.beginArray(); while (reader.hasNext()) { int id = -1; String title = null; int unread = 0; reader.beginObject(); while (reader.hasNext()) { try { switch (reader.nextName()) { case ID: id = reader.nextInt(); break; case TITLE: title = reader.nextString(); break; case UNREAD: unread = reader.nextInt(); break; default: reader.skipValue(); break; } } catch (IllegalArgumentException e) { e.printStackTrace(); reader.skipValue(); } } reader.endObject(); // Don't handle categories with an id below 1, we already have them in the DB from // Data.updateVirtualCategories() if (id > 0 && title != null) ret.add(new Category(id, title, unread)); } reader.endArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) try { reader.close(); } catch (IOException e1) { // Empty! } } Log.d(TAG, "getCategories: " + (System.currentTimeMillis() - time) + "ms"); return ret; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
/** * get current feeds from server/*from w w w .jav a2 s . c om*/ * * @param tolerateWrongUnreadInformation if set to {@code false}, then * lazy server will be updated before * @return set of actual feeds on server */ private Set<Feed> getFeeds(boolean tolerateWrongUnreadInformation) { long time = System.currentTimeMillis(); Set<Feed> ret = new LinkedHashSet<>(); if (sessionNotAlive()) return ret; if (!tolerateWrongUnreadInformation) { makeLazyServerWork(); } Map<String, String> params = new HashMap<>(); params.put(PARAM_OP, VALUE_GET_FEEDS); params.put(PARAM_CAT_ID, Data.VCAT_ALL + ""); // Hardcoded -4 fetches all feeds. See // http://tt-rss.org/redmine/wiki/tt-rss/JsonApiReference#getFeeds JsonReader reader = null; try { reader = prepareReader(params); if (reader == null) return ret; reader.beginArray(); while (reader.hasNext()) { int categoryId = -1; int id = 0; String title = null; String feedUrl = null; int unread = 0; reader.beginObject(); while (reader.hasNext()) { try { switch (reader.nextName()) { case ID: id = reader.nextInt(); break; case CAT_ID: categoryId = reader.nextInt(); break; case TITLE: title = reader.nextString(); break; case FEED_URL: feedUrl = reader.nextString(); break; case UNREAD: unread = reader.nextInt(); break; default: reader.skipValue(); break; } } catch (IllegalArgumentException e) { e.printStackTrace(); reader.skipValue(); } } reader.endObject(); if (id != -1 || categoryId == -2) // normal feed (>0) or label (-2) if (title != null) // Dont like complicated if-statements.. ret.add(new Feed(id, categoryId, title, feedUrl, unread)); } reader.endArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) try { reader.close(); } catch (IOException e1) { // Empty! } } Log.d(TAG, "getFeeds: " + (System.currentTimeMillis() - time) + "ms"); return ret; }
From source file:org.wso2.carbon.governance.rest.api.internal.JSONMessageBodyReader.java
License:Open Source License
/** * Traverses through a json object and maps the keys and values to a {@link Map} * * @param reader {@link JsonReader} * @param map map that the values to be added. * @param token {@link JsonToken} * @param isArray whether the object is inside a json array * @throws IOException If unable to parse the json object *///from w ww . j a v a2 s . c o m protected void handleObject(JsonReader reader, Map<String, Object> map, JsonToken token, boolean isArray) throws IOException { String key = null; while (true) { if (token == null) { token = reader.peek(); } if (JsonToken.BEGIN_OBJECT.equals(token)) { reader.beginObject(); } else if (JsonToken.END_OBJECT.equals(token)) { reader.endObject(); } else if (JsonToken.NAME.equals(token)) { key = reader.nextName(); } else if (JsonToken.STRING.equals(token)) { String value = reader.nextString(); handleValue(key, value, map, isArray); key = null; } else if (JsonToken.NUMBER.equals(token)) { Double value = reader.nextDouble(); handleValue(key, value, map, isArray); key = null; } else if (token.equals(JsonToken.BEGIN_ARRAY)) { Map<String, Object> values = handleArray(reader); if (key != null) { map.put(key, values); } } else { reader.skipValue(); } if (reader.hasNext()) { token = reader.peek(); } else { break; } } }
From source file:persistance.JSONHandler.java
private static Bee parseUnspecifiedBee(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; jsonReader.beginObject();/* www .jav a 2s .c om*/ while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "primaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); Random random = new Random(); if (random.nextInt(50) == 0) { return new Male(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); } return new Female(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); }
From source file:persistance.JSONHandler.java
private static Male parseDrone(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; jsonReader.beginObject();//from w w w . j a va 2s. c om while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "primaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); return new Male(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); }
From source file:persistance.JSONHandler.java
private static Female parsePrincess(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; jsonReader.beginObject();/*from w w w.ja va 2 s . c om*/ while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "primaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); return new Female(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); }
From source file:persistance.JSONHandler.java
private static Queen parseQueen(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; Female princess = null;/*from w w w .j av a 2 s .c o m*/ Male drone = null; jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "drone": drone = parseDrone(jsonReader); break; case "princess": princess = parsePrincess(jsonReader); break; case "primaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); Queen newQueen = new Queen(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); newQueen.setPrincess(princess); newQueen.setDrone(drone); return newQueen; }