Example usage for org.json JSONObject getInt

List of usage examples for org.json JSONObject getInt

Introduction

In this page you can find the example usage for org.json JSONObject getInt.

Prototype

public int getInt(String key) throws JSONException 

Source Link

Document

Get the int value associated with a key.

Usage

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private U2FContext parseU2FContextRegister(JSONObject json) {
    try {/*from  w w w. j  a va2s  .  c om*/
        byte[] challenge = null;
        String appId = json.getString(TAG_JSON_APPID);
        int requestId = json.getInt(TAG_JSON_REQUESTID);
        JSONArray array = json.getJSONArray(TAG_JSON_REGISTER_REQUESTS);
        for (int i = 0; i < array.length(); i++) {
            // TODO : only handle USB transport if several are present
            JSONObject registerItem = array.getJSONObject(i);
            if (!registerItem.getString(TAG_JSON_VERSION).equals(VERSION_U2F_V2)) {
                Log.e(TAG, "Invalid register version");
                return null;
            }
            challenge = Base64.decode(registerItem.getString(TAG_JSON_CHALLENGE), Base64.URL_SAFE);
        }
        return new U2FContext(appId, challenge, null, requestId, false);
    } catch (JSONException e) {
        Log.e(TAG, "Error decoding request");
        return null;
    }
}

From source file:com.jellymold.boss.ImageSearch.java

protected void parseResults(JSONObject jobj) throws JSONException {

    if (jobj != null) {

        setResponseCode(jobj.getInt("responsecode"));
        if (jobj.has("nextpage"))
            setNextPage(jobj.getString("nextpage"));
        if (jobj.has("prevpage"))
            setPrevPage(jobj.getString("prevpage"));
        setTotalResults(jobj.getLong("totalhits"));
        long count = jobj.getLong("count");
        setPagerCount(count);//from  w  ww.  j  av  a2 s. c o m
        setPagerStart(jobj.getLong("start"));
        this.setResults(new ArrayList<ImageSearchResult>((int) count));

        if (jobj.has("resultset_images")) {

            JSONArray res = jobj.getJSONArray("resultset_images");
            for (int i = 0; i < res.length(); i++) {
                JSONObject thisResult = res.getJSONObject(i);
                ImageSearchResult imageSearchResult = new ImageSearchResult();
                imageSearchResult.setDescription(thisResult.getString("abstract"));
                imageSearchResult.setClickUrl(thisResult.getString("clickurl"));
                imageSearchResult.setDate(thisResult.getString("date"));
                imageSearchResult.setTitle(thisResult.getString("title"));
                imageSearchResult.setUrl(thisResult.getString("url"));
                imageSearchResult.setSize(thisResult.getLong("size"));
                imageSearchResult.setFilename(thisResult.getString("filename"));
                imageSearchResult.setFormat(thisResult.getString("format"));
                imageSearchResult.setHeight(thisResult.getLong("height"));
                imageSearchResult.setMimeType(thisResult.getString("mimetype"));
                imageSearchResult.setRefererClickUrl(thisResult.getString("refererclickurl"));
                imageSearchResult.setRefererUrl(thisResult.getString("refererurl"));
                imageSearchResult.setThumbnailHeight(thisResult.getLong("thumbnail_height"));
                imageSearchResult.setThumbnailWidth(thisResult.getLong("thumbnail_width"));
                imageSearchResult.setThumbnailUrl(thisResult.getString("thumbnail_url"));
                this.getResults().add(imageSearchResult);
            }
        }
    }

}

From source file:edu.txstate.dmlab.clusteringwiki.sources.AbsSearchResultCol.java

/**
 * Creates a collection of results from JSON response
 * Note that firstPosition must be set before adding
 * results as result ids depend on that value.
 * @param res//from   w w  w.j  a  va  2 s.co  m
 */
public AbsSearchResultCol(JSONObject res) {

    if (res == null)
        return;
    JSONObject search = null;

    try {
        search = res.getJSONObject("search");
        JSONArray errors = search.getJSONArray("errors");
        if (errors != null && errors.length() > 0) {
            for (int i = 0; i < errors.length(); i++) {
                String error = errors.getString(i);
                addError("AbS API exception: " + error);
            }
            return;
        }
    } catch (JSONException e) {
        addError("AbS API exception: " + e.getMessage());
    }

    try {
        totalResults = search.getInt("totalResults");
        firstPosition = search.getInt("firstPosition");

        JSONArray j = search.getJSONArray("results");
        returnedCount = j.length();

        for (int i = 0; i < j.length(); i++) {
            ICWSearchResult r = new AbsSearchResult(j.getJSONObject(i));
            r.setIndex(i);
            addResult(r);
        }

    } catch (JSONException e) {
        addError("Could not retrieve AbS results: " + e.getMessage());
    }
}

From source file:com.norman0406.slimgress.API.Knobs.ClientFeatureKnobs.java

public ClientFeatureKnobs(JSONObject json) throws JSONException {
    super(json);//from  www  .  j  a v a 2  s  .c o m

    mEnableInviteNag = json.getBoolean("enableInviteNag");
    mFireMode = json.getInt("fireMode");
    mEnableRecycle = json.getBoolean("enableRecycle");
    mInviteNagDelayDays = json.getInt("inviteNagDelayDays");
    mRefreshTimeMS = json.getInt("refreshTimeMs");
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Fills articles in index.ftl.//  w  w w.j a  va  2  s  .c  o  m
 *
 * @param dataModel data model
 * @param currentPageNum current page number
 * @param preference the specified preference
 * @throws ServiceException service exception
 */
public void fillIndexArticles(final Map<String, Object> dataModel, final int currentPageNum,
        final JSONObject preference) throws ServiceException {
    Stopwatchs.start("Fill Index Articles");

    try {
        final int pageSize = preference.getInt(Preference.ARTICLE_LIST_DISPLAY_COUNT);
        final int windowSize = preference.getInt(Preference.ARTICLE_LIST_PAGINATION_WINDOW_SIZE);

        final JSONObject statistic = statisticQueryService.getStatistic();
        final int publishedArticleCnt = statistic.getInt(Statistic.STATISTIC_PUBLISHED_ARTICLE_COUNT);
        final int pageCount = (int) Math.ceil((double) publishedArticleCnt / (double) pageSize);

        final Query query = new Query().setCurrentPageNum(currentPageNum).setPageSize(pageSize)
                .setPageCount(pageCount)
                .setFilter(new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, PUBLISHED))
                .addSort(Article.ARTICLE_PUT_TOP, SortDirection.DESCENDING).index(Article.ARTICLE_PERMALINK);

        if (preference.getBoolean(Preference.ENABLE_ARTICLE_UPDATE_HINT)) {
            query.addSort(Article.ARTICLE_UPDATE_DATE, SortDirection.DESCENDING);
        } else {
            query.addSort(Article.ARTICLE_CREATE_DATE, SortDirection.DESCENDING);
        }

        final JSONObject result = articleRepository.get(query);
        final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
        if (0 != pageNums.size()) {
            dataModel.put(Pagination.PAGINATION_FIRST_PAGE_NUM, pageNums.get(0));
            dataModel.put(Pagination.PAGINATION_LAST_PAGE_NUM, pageNums.get(pageNums.size() - 1));
        }

        dataModel.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
        dataModel.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);

        final List<JSONObject> articles = org.b3log.latke.util.CollectionUtils
                .jsonArrayToList(result.getJSONArray(Keys.RESULTS));

        final boolean hasMultipleUsers = Users.getInstance().hasMultipleUsers();
        if (hasMultipleUsers) {
            setArticlesExProperties(articles, preference);
        } else {
            if (!articles.isEmpty()) {
                final JSONObject author = articleUtils.getAuthor(articles.get(0));
                setArticlesExProperties(articles, author, preference);
            }
        }

        dataModel.put(Article.ARTICLES, articles);
    } catch (final JSONException e) {
        LOGGER.log(Level.SEVERE, "Fills index articles failed", e);
        throw new ServiceException(e);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.SEVERE, "Fills index articles failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Fills most used tags./*from ww w  . ja v a 2 s .co m*/
 *
 * @param dataModel data model
 * @param preference the specified preference
 * @throws ServiceException service exception
 */
public void fillMostUsedTags(final Map<String, Object> dataModel, final JSONObject preference)
        throws ServiceException {
    Stopwatchs.start("Fill Most Used Tags");

    try {
        LOGGER.finer("Filling most used tags....");
        final int mostUsedTagDisplayCnt = preference.getInt(Preference.MOST_USED_TAG_DISPLAY_CNT);

        final List<JSONObject> tags = tagRepository.getMostUsedTags(mostUsedTagDisplayCnt);
        tagUtils.removeForUnpublishedArticles(tags);

        dataModel.put(Common.MOST_USED_TAGS, tags);
    } catch (final JSONException e) {
        LOGGER.log(Level.SEVERE, "Fills most used tags failed", e);
        throw new ServiceException(e);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.SEVERE, "Fills most used tags failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Fills most view count articles.//from  ww w  . j  a  v  a 2 s. com
 *
 * @param dataModel data model
 * @param preference the specified preference
 * @throws ServiceException service exception
 */
public void fillMostViewCountArticles(final Map<String, Object> dataModel, final JSONObject preference)
        throws ServiceException {
    Stopwatchs.start("Fill Most View Articles");
    try {
        LOGGER.finer("Filling the most view count articles....");
        final int mostCommentArticleDisplayCnt = preference.getInt(Preference.MOST_VIEW_ARTICLE_DISPLAY_CNT);
        final List<JSONObject> mostViewCountArticles = articleRepository
                .getMostViewCountArticles(mostCommentArticleDisplayCnt);

        dataModel.put(Common.MOST_VIEW_COUNT_ARTICLES, mostViewCountArticles);

    } catch (final Exception e) {
        LOGGER.log(Level.SEVERE, "Fills most view count articles failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Fills most comments articles./* www . j a va2  s.c  om*/
 *
 * @param dataModel data model
 * @param preference the specified preference
 * @throws ServiceException service exception
 */
public void fillMostCommentArticles(final Map<String, Object> dataModel, final JSONObject preference)
        throws ServiceException {
    Stopwatchs.start("Fill Most CMMTs Articles");

    try {
        LOGGER.finer("Filling most comment articles....");
        final int mostCommentArticleDisplayCnt = preference.getInt(Preference.MOST_COMMENT_ARTICLE_DISPLAY_CNT);
        final List<JSONObject> mostCommentArticles = articleRepository
                .getMostCommentArticles(mostCommentArticleDisplayCnt);

        dataModel.put(Common.MOST_COMMENT_ARTICLES, mostCommentArticles);
    } catch (final Exception e) {
        LOGGER.log(Level.SEVERE, "Fills most comment articles failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Fills post articles recently.//from ww w.j ava  2  s . co  m
 *
 * @param dataModel data model
 * @param preference the specified preference
 * @throws ServiceException service exception
 */
public void fillRecentArticles(final Map<String, Object> dataModel, final JSONObject preference)
        throws ServiceException {
    Stopwatchs.start("Fill Recent Articles");

    try {
        final int recentArticleDisplayCnt = preference.getInt(Preference.RECENT_ARTICLE_DISPLAY_CNT);

        final List<JSONObject> recentArticles = articleRepository.getRecentArticles(recentArticleDisplayCnt);

        dataModel.put(Common.RECENT_ARTICLES, recentArticles);

    } catch (final JSONException e) {
        LOGGER.log(Level.SEVERE, "Fills recent articles failed", e);
        throw new ServiceException(e);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.SEVERE, "Fills recent articles failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Fills post comments recently.//from   w ww  .j ava 2s .c  o m
 *
 * @param dataModel data model
 * @param preference the specified preference
 * @throws ServiceException service exception
 */
public void fillRecentComments(final Map<String, Object> dataModel, final JSONObject preference)
        throws ServiceException {
    Stopwatchs.start("Fill Recent Comments");
    try {
        LOGGER.finer("Filling recent comments....");
        final int recentCommentDisplayCnt = preference.getInt(Preference.RECENT_COMMENT_DISPLAY_CNT);

        final List<JSONObject> recentComments = commentRepository.getRecentComments(recentCommentDisplayCnt);

        for (final JSONObject comment : recentComments) {
            final String content = comment.getString(Comment.COMMENT_CONTENT)
                    .replaceAll(SoloServletListener.ENTER_ESC, "&nbsp;");
            comment.put(Comment.COMMENT_CONTENT, content);
            comment.put(Comment.COMMENT_NAME,
                    StringEscapeUtils.escapeHtml(comment.getString(Comment.COMMENT_NAME)));
            comment.put(Comment.COMMENT_URL,
                    StringEscapeUtils.escapeHtml(comment.getString(Comment.COMMENT_URL)));

            comment.remove(Comment.COMMENT_EMAIL); // Erases email for security reason
        }

        dataModel.put(Common.RECENT_COMMENTS, recentComments);

    } catch (final JSONException e) {
        LOGGER.log(Level.SEVERE, "Fills recent comments failed", e);
        throw new ServiceException(e);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.SEVERE, "Fills recent comments failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}