List of usage examples for com.google.gson.stream JsonReader close
public void close() throws IOException
From source file:org.terasology.rendering.gui.components.UITextWrap.java
License:Apache License
public int getLineCount() throws IOException { Gson gson = new Gson(); JsonReader reader = new JsonReader(new FileReader(".\\data\\console\\consolelog.json")); int counter = 0; reader.beginArray();/*from ww w .j a v a2 s . com*/ while (reader.hasNext()) { counter++; gson.fromJson(reader, String.class); } reader.endArray(); reader.close(); return counter; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
private String readResult(Map<String, String> params, boolean login, boolean retry) throws IOException { InputStream in = doRequest(params); if (in == null) return null; JsonReader reader = null; String ret = ""; try {//from ww w .j a v a 2s. c o m reader = new JsonReader(new InputStreamReader(in, "UTF-8")); // Check if content contains array or object, array indicates login-response or error, object is content reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (!name.equals("content")) { reader.skipValue(); continue; } JsonToken t = reader.peek(); if (!t.equals(JsonToken.BEGIN_OBJECT)) continue; JsonObject object = new JsonObject(); reader.beginObject(); while (reader.hasNext()) { object.addProperty(reader.nextName(), reader.nextString()); } reader.endObject(); if (object.get(SESSION_ID) != null) { ret = object.get(SESSION_ID).getAsString(); } if (object.get(STATUS) != null) { ret = object.get(STATUS).getAsString(); } if (this.apiLevel == -1 && object.get(API_LEVEL) != null) { this.apiLevel = object.get(API_LEVEL).getAsInt(); } if (object.get(VALUE) != null) { ret = object.get(VALUE).getAsString(); } if (object.get(ERROR) != null) { String message = object.get(ERROR).getAsString(); Context ctx = MyApplication.context(); switch (message) { case API_DISABLED: lastError = ctx.getString(R.string.Error_ApiDisabled, Controller.getInstance().username()); break; case NOT_LOGGED_IN: case LOGIN_ERROR: if (!login && retry && login()) return readResult(params, false, false); // Just do the same request again else lastError = ctx.getString(R.string.Error_LoginFailed); break; case INCORRECT_USAGE: lastError = ctx.getString(R.string.Error_ApiIncorrectUsage); break; case UNKNOWN_METHOD: lastError = ctx.getString(R.string.Error_ApiUnknownMethod); break; default: lastError = ctx.getString(R.string.Error_ApiUnknownError); break; } hasLastError = true; Log.e(TAG, message); return null; } } } finally { if (reader != null) reader.close(); } if (ret.startsWith("\"")) ret = ret.substring(1, ret.length()); if (ret.endsWith("\"")) ret = ret.substring(0, ret.length() - 1); return ret; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
/** * Retrieves all categories.// w ww. j a va2 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 . j av a 2 s. c o m*/ * * @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.ttrssreader.net.JSONConnector.java
License:Open Source License
/** * Retrieves the specified articles./*from ww w.j a va 2s . co m*/ * * @param articles container for retrieved articles * @param id the id of the feed/category * @param limit the maximum number of articles to be fetched * @param viewMode indicates wether only unread articles should be included (Possible values: all_articles, * unread, * adaptive, marked, updated) * @param isCategory indicates if we are dealing with a category or a feed * @param sinceId the first ArticleId which is to be retrieved. * @param search search query * @param skipProperties set of article fields, which should not be parsed (may be {@code null}) * @param filter filter for articles, defining which articles should be omitted while parsing (may be * {@code * null}) */ public void getHeadlines(final Set<Article> articles, Integer id, int limit, String viewMode, boolean isCategory, Integer sinceId, String search, Set<Article.ArticleField> skipProperties, IArticleOmitter filter) { long time = System.currentTimeMillis(); int offset = 0; int count; int maxSize = articles.size() + limit; if (sessionNotAlive()) return; int limitParam = Math.min((apiLevel < 6) ? PARAM_LIMIT_API_5 : PARAM_LIMIT_MAX_VALUE, limit); makeLazyServerWork(id); while (articles.size() < maxSize) { Map<String, String> params = new HashMap<>(); params.put(PARAM_OP, VALUE_GET_HEADLINES); params.put(PARAM_FEED_ID, id + ""); params.put(PARAM_LIMIT, limitParam + ""); params.put(PARAM_SKIP, offset + ""); params.put(PARAM_VIEWMODE, viewMode); params.put(PARAM_IS_CAT, (isCategory ? "1" : "0")); if (skipProperties == null || !skipProperties.contains(Article.ArticleField.content)) params.put(PARAM_SHOW_CONTENT, "1"); if (skipProperties == null || !skipProperties.contains(Article.ArticleField.attachments)) params.put(PARAM_INC_ATTACHMENTS, "1"); if (sinceId > 0) params.put(PARAM_SINCE_ID, sinceId + ""); if (search != null) params.put(PARAM_SEARCH, search); JsonReader reader = null; try { reader = prepareReader(params); if (hasLastError) return; if (reader == null) continue; count = parseArticleArray(articles, reader, skipProperties, filter); if (count < limitParam) break; else offset += count; } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException ignored) { // Empty! } } } } Log.d(TAG, "getHeadlines: " + (System.currentTimeMillis() - time) + "ms"); }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
public SubscriptionResponse feedSubscribe(String feed_url, int category_id) { SubscriptionResponse ret = new SubscriptionResponse(); if (sessionNotAlive()) return ret; Map<String, String> params = new HashMap<>(); params.put(PARAM_OP, VALUE_FEED_SUBSCRIBE); params.put(PARAM_FEED_URL, feed_url); params.put(PARAM_CATEGORY_ID, category_id + ""); String code = ""; String message = null;//from w w w . j a va2 s . c om JsonReader reader = null; try { reader = prepareReader(params); if (reader == null) return ret; reader.beginObject(); while (reader.hasNext()) { switch (reader.nextName()) { case "code": code = reader.nextString(); break; case "message": message = reader.nextString(); break; default: reader.skipValue(); break; } } if (!code.contains(UNKNOWN_METHOD)) { ret.code = Integer.parseInt(code); ret.message = message; } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) try { reader.close(); } catch (IOException e1) { // Empty! } } return ret; }
From source file:org.unitime.timetable.api.JsonApiHelper.java
License:Apache License
@Override public <P> P getRequest(Type requestType) throws IOException { if (iGson == null) iGson = createGson();//from w ww.j a va 2 s .c o m JsonReader reader = new JsonReader(iRequest.getReader()); try { return iGson.fromJson(reader, requestType); } finally { reader.close(); } }
From source file:org.unitime.timetable.onlinesectioning.custom.purdue.GsonRepresentation.java
License:Apache License
public T getObject() throws IOException { if (iObject == null && iRepresentation != null && iRepresentation.isAvailable()) { JsonReader reader = new JsonReader(iRepresentation.getReader()); try {//from ww w .jav a2 s. c o m if (iObjectType != null) return getBuilder().create().fromJson(reader, iObjectType); else return getBuilder().create().fromJson(reader, iObjectClass); } finally { reader.close(); iRepresentation.release(); } } return iObject; }
From source file:org.unitime.timetable.onlinesectioning.custom.purdue.XEStudentEnrollment.java
License:Open Source License
protected <T> T readResponse(Gson gson, Response response, Type typeOfT) throws JsonIOException, JsonSyntaxException, IOException { if (response == null) return null; JsonReader reader = new JsonReader(response.getEntity().getReader()); try {/*www . ja v a 2s . c om*/ return gson.fromJson(reader, typeOfT); } finally { reader.close(); response.release(); } }
From source file:project.latex.balloon.BalloonController.java
static List<String> loadTransmittedDataKeys(String filePath) throws IOException { if (filePath == null) { throw new IllegalArgumentException("Cannot load keys from null file"); }/*from w ww . ja v a2 s .c o m*/ JsonReader reader = null; try { List<String> dataKeys = new ArrayList<>(); reader = new JsonReader(new FileReader(filePath)); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); reader.beginArray(); while (reader.hasNext()) { dataKeys.add(reader.nextString()); } reader.endArray(); } reader.endObject(); reader.close(); return dataKeys; } finally { if (reader != null) { reader.close(); } } }