Example usage for org.json JSONArray length

List of usage examples for org.json JSONArray length

Introduction

In this page you can find the example usage for org.json JSONArray length.

Prototype

public int length() 

Source Link

Document

Get the number of elements in the JSONArray, included nulls.

Usage

From source file:com.miz.service.TraktMoviesSyncService.java

private void downloadMovieFavorites() {
    JSONArray jsonArray = Trakt.getMovieLibrary(this, Trakt.RATINGS);
    if (jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                String tmdbId = String.valueOf(jsonArray.getJSONObject(i).get("tmdb_id"));
                mMovieFavorites.add(tmdbId);
                mMovieDatabase.updateMovieSingleItem(tmdbId, DbAdapterMovies.KEY_FAVOURITE, "1");
            } catch (Exception e) {
            }/*  ww w . j a va 2 s. c om*/
        }
    }
}

From source file:com.miz.service.TraktMoviesSyncService.java

private void downloadWatchlist() {
    JSONArray jsonArray = Trakt.getMovieLibrary(this, Trakt.WATCHLIST);
    if (jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                String tmdbId = String.valueOf(jsonArray.getJSONObject(i).get("tmdb_id"));
                mWatchlist.add(tmdbId);//from ww  w .  ja  v a2  s . co m
                mMovieDatabase.updateMovieSingleItem(tmdbId, DbAdapterMovies.KEY_TO_WATCH, "1");
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.github.koraktor.steamcondenser.community.GameInventory.java

/**
 * Updates the contents of the backpack using Steam Web API
 *
 * @throws WebApiException on Web API errors
 *//*from   w  w w . j  a v  a 2s .co  m*/
public void fetch() throws SteamCondenserException {
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("SteamID", this.steamId64);
        JSONObject result = WebApi.getJSONData("IEconItems_" + this.getAppId(), "GetPlayerItems", 1, params);

        this.items = new HashMap<Integer, GameItem>();
        this.preliminaryItems = new ArrayList<GameItem>();
        JSONArray itemsData = result.getJSONArray("items");
        for (int i = 0; i < itemsData.length(); i++) {
            JSONObject itemData = itemsData.getJSONObject(i);
            if (itemData != null) {
                try {
                    GameItem item = this.getItemClass().getConstructor(this.getClass(), JSONObject.class)
                            .newInstance(this, itemData);
                    if (item.isPreliminary()) {
                        this.preliminaryItems.add(item);
                    } else {
                        this.items.put(item.getBackpackPosition() - 1, item);
                    }
                } catch (IllegalAccessException e) {
                } catch (InstantiationException e) {
                } catch (InvocationTargetException e) {
                    if (e.getCause() instanceof SteamCondenserException) {
                        throw (SteamCondenserException) e.getCause();
                    } else {
                        throw (RuntimeException) e.getCause();
                    }
                } catch (NoSuchMethodException e) {
                }
            }
        }
    } catch (JSONException e) {
        throw new WebApiException("Could not parse JSON data.", e);
    }

    this.fetchDate = new Date();
}

From source file:com.tonikorin.cordova.plugin.LocationProvider.LocationService.java

private void updateLocateHistory(JSONObject messageIn, boolean blocked, String msgType, String time)
        throws JSONException { // Read current history
    SharedPreferences sp = myContext.getSharedPreferences(LocationService.PREFS_NAME, Context.MODE_PRIVATE);
    String historyJsonStr = sp.getString(LocationService.HISTORY_NAME, "{}");
    JSONObject history = new JSONObject(historyJsonStr);
    if (CHAT.equals(msgType)) { // CHAT history, save hole message
        JSONArray chatMessages = history.optJSONArray("chatMessages");
        if (chatMessages == null)
            chatMessages = new JSONArray();
        //Log.d(TAG, "CHAT history TIME: " + time);
        messageIn.put("time", Long.parseLong(time));
        chatMessages.put(messageIn.toString());
        if (chatMessages.length() > 100)
            chatMessages.remove(0); // store only last 100 chat messages
        history.put("chatMessages", chatMessages);
    } else {// Current LOCATE
        String member = messageIn.optString("memberName", "");
        if (blocked)
            member = "\u2717 " + member;
        JSONObject updateStatus = new JSONObject();
        updateStatus.put("member", member);
        updateStatus.put("team", messageIn.optString("teamId", ""));
        updateStatus.put("date", getDateAndTimeString(System.currentTimeMillis()));
        updateStatus.put("target", messageIn.optString("target", ""));
        history.put("updateStatus", updateStatus);
        // History LOCATE lines...
        JSONArray historyLines = history.optJSONArray("lines");
        if (historyLines == null)
            historyLines = new JSONArray();
        String target;/*from   w  w w.  j  a  va 2s.  c om*/
        if (updateStatus.getString("target").equals(""))
            target = updateStatus.optString("team");
        else
            target = updateStatus.optString("target");
        String historyLine = updateStatus.getString("member") + " (" + target + ") "
                + updateStatus.getString("date") + "\n";
        historyLines.put(historyLine);
        if (historyLines.length() > 200)
            historyLines.remove(0); // store only last 200 locate queries
        history.put("lines", historyLines);
    }
    // Save new history
    SharedPreferences.Editor editor = sp.edit();
    editor.putString(LocationService.HISTORY_NAME, history.toString());
    editor.commit();
    //Log.d(TAG, "history:" + history.toString());
}

From source file:com.github.koraktor.steamcondenser.community.SteamId.java

/**
 * Fetches the friends of this user/* w  ww. ja va 2 s  . c  o m*/
 * <p>
 * This creates a new <code>SteamId</code> instance for each of the friends
 * without fetching their data.
 *
 * @see #getFriends
 * @see SteamId#SteamId
 * @throws SteamCondenserException if an error occurs while parsing the
 *         data
 */
private void fetchFriends() throws SteamCondenserException {
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("relationship", "friend");
        params.put("steamid", this.steamId64);

        JSONObject jsonData = new JSONObject(WebApi.getJSON("ISteamUser", "GetFriendList", 1, params));
        JSONArray friendsData = jsonData.getJSONObject("friendslist").getJSONArray("friends");
        this.friends = new ArrayList<SteamId>();
        for (int i = 0; i < friendsData.length(); i++) {
            JSONObject friend = friendsData.getJSONObject(i);
            this.friends.add(new SteamId(friend.getLong("steamid"), false));
        }
    } catch (JSONException e) {
        throw new WebApiException("Could not parse JSON data.", e);
    }
}

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

public Project[] getProjects() throws IOException, ElementNotFoundException {
    String body = httpGet("/index.php?/api/v2/get_projects").getBody();
    JSONArray json = new JSONArray(body);
    Project[] projects = new Project[json.length()];
    for (int i = 0; i < json.length(); i++) {
        JSONObject o = json.getJSONObject(i);
        Project p = new Project();
        p.setName(o.getString("name"));
        p.setId(o.getInt("id"));
        projects[i] = p;//  w  ww  .  ja v  a 2  s. com
    }
    return projects;
}

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

public Suite[] getSuites(int projectId) throws IOException, ElementNotFoundException {
    String body = httpGet("/index.php?/api/v2/get_suites/" + projectId).getBody();

    JSONArray json;
    try {/*w  w  w.java  2s  .  c  o m*/
        json = new JSONArray(body);
    } catch (JSONException e) {
        return new Suite[0];
    }

    Suite[] suites = new Suite[json.length()];
    for (int i = 0; i < json.length(); i++) {
        JSONObject o = json.getJSONObject(i);
        Suite s = new Suite();
        s.setName(o.getString("name"));
        s.setId(o.getInt("id"));
        suites[i] = s;
    }

    return suites;
}

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

public Case[] getCases(int projectId, int suiteId) throws IOException, ElementNotFoundException {
    // "/#{project_id}&suite_id=#{suite_id}#{section_string}"
    String body = httpGet("index.php?/api/v2/get_cases/" + projectId + "&suite_id=" + suiteId).getBody();
    JSONArray json = new JSONArray(body);

    Case[] cases = new Case[json.length()];
    for (int i = 0; i < json.length(); i++) {
        JSONObject o = json.getJSONObject(i);
        cases[i] = createCaseFromJson(o);
    }//  ww  w  . ja  va2 s  .  c  om

    return cases;
}

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

public Section[] getSections(int projectId, int suiteId) throws IOException, ElementNotFoundException {
    String body = httpGet("index.php?/api/v2/get_sections/" + projectId + "&suite_id=" + suiteId).getBody();
    JSONArray json = new JSONArray(body);

    Section[] sects = new Section[json.length()];
    for (int i = 0; i < json.length(); i++) {
        JSONObject o = json.getJSONObject(i);
        sects[i] = createSectionFromJSON(o);
    }//from   ww w. j a  v  a2  s . c  o m

    return sects;
}

From source file:org.jenkinsci.plugins.testrail.TestRailClient.java

public Milestone[] getMilestones(int projectId) throws IOException, ElementNotFoundException {
    String body = httpGet("index.php?/api/v2/get_milestones/" + projectId).getBody();
    JSONArray json;
    try {/*w  w  w  .  ja  va  2 s .  com*/
        json = new JSONArray(body);
    } catch (JSONException e) {
        return new Milestone[0];
    }
    Milestone[] suites = new Milestone[json.length()];
    for (int i = 0; i < json.length(); i++) {
        JSONObject o = json.getJSONObject(i);
        Milestone s = new Milestone();
        s.setName(o.getString("name"));
        s.setId(String.valueOf(o.getInt("id")));
        suites[i] = s;
    }
    return suites;
}