Example usage for org.json JSONArray put

List of usage examples for org.json JSONArray put

Introduction

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

Prototype

public JSONArray put(Object value) 

Source Link

Document

Append an object value.

Usage

From source file:com.miz.apis.trakt.Trakt.java

public static boolean markEpisodeAsWatched(String showId, List<com.miz.functions.TvShowEpisode> episodes,
        Context c, boolean watched) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)
            || !settings.getBoolean(SYNC_WITH_TRAKT, false) || episodes.size() == 0)
        return false;

    try {//from   ww w .  j  ava2s.c  o  m
        JSONObject holder = new JSONObject();
        holder.put("username", username);
        holder.put("password", password);
        holder.put("imdb_id", "");
        holder.put("tvdb_id", showId);
        holder.put("title", "");
        holder.put("year", "");

        JSONArray array = new JSONArray();
        int count = episodes.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonMovie = new JSONObject();
            jsonMovie.put("season", episodes.get(i).getSeason());
            jsonMovie.put("episode", episodes.get(i).getEpisode());
            array.put(jsonMovie);
        }
        holder.put("episodes", array);

        Request request = MizLib.getJsonPostRequest(
                "http://api.trakt.tv/show/episode/" + (!watched ? "un" : "") + "seen/" + getApiKey(c), holder);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();

        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

public static boolean markTvShowAsWatched(TraktTvShow show, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password))
        return false;

    try {//from w w w .jav  a 2s . com
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);
        json.put("tvdb_id", show.getId());
        json.put("title", show.getTitle());

        JSONArray array = new JSONArray();
        for (String season : show.getSeasons().keySet()) {
            Collection<String> episodes = show.getSeasons().get(season);
            for (String episode : episodes) {
                JSONObject jsonShow = new JSONObject();
                jsonShow.put("season", season);
                jsonShow.put("episode", episode);
                array.put(jsonShow);
            }
        }
        json.put("episodes", array);

        Request request = MizLib.getJsonPostRequest("http://api.trakt.tv/show/episode/seen/" + getApiKey(c),
                json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

public static boolean addMoviesToLibrary(List<Movie> movies, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || movies.size() == 0)
        return false;

    try {/*from   w  w  w.  j  a v a  2  s.  c  om*/
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);

        JSONArray array = new JSONArray();
        int count = movies.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonMovie = new JSONObject();
            jsonMovie.put("imdb_id", movies.get(i).getImdbId());
            jsonMovie.put("tmdb_id", movies.get(i).getTmdbId());
            jsonMovie.put("year", movies.get(i).getReleaseYear());
            jsonMovie.put("title", movies.get(i).getTitle());
            array.put(jsonMovie);
        }
        json.put("movies", array);

        Request request = MizLib.getJsonPostRequest("http://api.trakt.tv/movie/library/" + getApiKey(c), json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

public static boolean movieWatchlist(List<Movie> movies, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || movies.size() == 0)
        return false;

    try {//from w w w  .  jav a2 s  .com
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);

        JSONArray array = new JSONArray();
        int count = movies.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonMovie = new JSONObject();
            jsonMovie.put("imdb_id", movies.get(i).getImdbId());
            jsonMovie.put("tmdb_id", movies.get(i).getTmdbId());
            jsonMovie.put("year", movies.get(i).getReleaseYear());
            jsonMovie.put("title", movies.get(i).getTitle());
            array.put(jsonMovie);
        }
        json.put("movies", array);

        Request request = MizLib
                .getJsonPostRequest((movies.get(0).toWatch() ? "http://api.trakt.tv/movie/watchlist/"
                        : "http://api.trakt.tv/movie/unwatchlist/") + getApiKey(c), json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

public static boolean moviesWatchlist(List<MediumMovie> movies, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || movies.size() == 0)
        return false;

    try {//from   ww  w  . ja  v a2s .  c o m
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);

        JSONArray array = new JSONArray();
        int count = movies.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonMovie = new JSONObject();
            jsonMovie.put("tmdb_id", movies.get(i).getTmdbId());
            jsonMovie.put("year", movies.get(i).getReleaseYear());
            jsonMovie.put("title", movies.get(i).getTitle());
            array.put(jsonMovie);
        }
        json.put("movies", array);

        Request request = MizLib
                .getJsonPostRequest((movies.get(0).toWatch() ? "http://api.trakt.tv/movie/watchlist/"
                        : "http://api.trakt.tv/movie/unwatchlist/") + getApiKey(c), json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

public static boolean movieFavorite(List<Movie> movies, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || movies.size() == 0)
        return false;

    try {/*from  ww  w .j  a va2s  .  c om*/
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);

        JSONArray array = new JSONArray();
        int count = movies.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonMovie = new JSONObject();
            jsonMovie.put("imdb_id", movies.get(i).getImdbId());
            jsonMovie.put("tmdb_id", movies.get(i).getTmdbId());
            jsonMovie.put("year", movies.get(i).getReleaseYear());
            jsonMovie.put("title", movies.get(i).getTitle());
            jsonMovie.put("rating", movies.get(i).isFavourite() ? "love" : "unrate");
            array.put(jsonMovie);
        }
        json.put("movies", array);

        Request request = MizLib.getJsonPostRequest("http://api.trakt.tv/rate/movies/" + getApiKey(c), json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

public static boolean moviesFavorite(List<MediumMovie> movies, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || movies.size() == 0)
        return false;

    try {/*from  ww  w  .j a v  a2 s.com*/
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);

        JSONArray array = new JSONArray();
        int count = movies.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonMovie = new JSONObject();
            jsonMovie.put("tmdb_id", movies.get(i).getTmdbId());
            jsonMovie.put("year", movies.get(i).getReleaseYear());
            jsonMovie.put("title", movies.get(i).getTitle());
            jsonMovie.put("rating", movies.get(i).isFavourite() ? "love" : "unrate");
            array.put(jsonMovie);
        }
        json.put("movies", array);

        Request request = MizLib.getJsonPostRequest("http://api.trakt.tv/rate/movies/" + getApiKey(c), json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

public static boolean addTvShowToLibrary(TraktTvShow show, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password))
        return false;

    try {//ww  w . j  av  a  2  s .  com
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);
        json.put("tvdb_id", show.getId());
        json.put("title", show.getTitle());

        JSONArray array = new JSONArray();
        for (String season : show.getSeasons().keySet()) {
            Collection<String> episodes = show.getSeasons().get(season);
            for (String episode : episodes) {
                JSONObject jsonShow = new JSONObject();
                jsonShow.put("season", season);
                jsonShow.put("episode", episode);
                array.put(jsonShow);
            }
        }
        json.put("episodes", array);

        Request request = MizLib.getJsonPostRequest("http://api.trakt.tv/show/episode/library/" + getApiKey(c),
                json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

public static boolean tvShowFavorite(List<TvShow> shows, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || shows.size() == 0)
        return false;

    try {/* w w w.jav  a 2s .co  m*/
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);

        JSONArray array = new JSONArray();
        int count = shows.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonShow = new JSONObject();
            jsonShow.put("tvdb_id", shows.get(i).getId());
            jsonShow.put("title", shows.get(i).getTitle());
            jsonShow.put("rating", shows.get(i).isFavorite() ? "love" : "unrate");
            array.put(jsonShow);
        }
        json.put("shows", array);

        Request request = MizLib.getJsonPostRequest("http://api.trakt.tv/rate/shows/" + getApiKey(c), json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:org.schedulesdirect.grabber.ScheduleTask.java

protected void fetchStations(Map<String, Collection<String>> ids) {
    if (ids == null || ids.size() == 0) {
        LOG.info("No stale schedules identified; skipping schedule download!");
        return;/*from  w  w w .  j  a v a2  s.c  om*/
    }
    DefaultJsonRequest req = factory.get(DefaultJsonRequest.Action.POST, RestNouns.SCHEDULES, clnt.getHash(),
            clnt.getUserAgent(), clnt.getBaseUrl());
    JSONArray data = new JSONArray();
    Iterator<String> idItr = ids.keySet().iterator();
    while (idItr.hasNext()) {
        String id = idItr.next();
        JSONObject o = new JSONObject();
        o.put("stationID", id);
        Collection<String> dates = new ArrayList<>();
        for (String date : ids.get(id))
            dates.add(date);
        o.put("date", dates);
        data.put(o);
    }
    try {
        JSONArray resp = Config.get().getObjectMapper().readValue(req.submitForJson(data), JSONArray.class);
        for (int i = 0; i < resp.length(); ++i) {
            JSONObject o = resp.getJSONObject(i);
            if (!JsonResponseUtils.isErrorResponse(o)) {
                JSONArray sched = o.getJSONArray("programs");
                String schedId = o.getString("stationID");
                Date expiry = new Date(System.currentTimeMillis() - Grabber.MAX_AIRING_AGE);
                for (int j = 0; j < sched.length(); ++j) {
                    try {
                        JSONObject airing = sched.getJSONObject(j);
                        Date end = AiringUtils.getEndDate(airing);
                        String progId = airing.getString("programID");
                        if (!end.before(expiry)) {
                            String md5 = airing.getString("md5");
                            cache.markIfDirty(progId, md5);
                        } else
                            LOG.debug(String.format("Expired airing discovered and ignored! [%s; %s; %s]",
                                    progId, o.getString("stationID"), end));
                        synchronized (ScheduleTask.class) {
                            List<JSONObject> objs = FULL_SCHEDS.get(schedId);
                            if (objs == null) {
                                objs = new ArrayList<JSONObject>();
                                FULL_SCHEDS.put(schedId, objs);
                            }
                            objs.add(airing);
                        }
                    } catch (JSONException e) {
                        LOG.warn(String.format("JSONException [%s]", o.optString("stationID", "unknown")), e);
                    }
                }
            } else if (JsonResponseUtils.getErrorCode(o) == ApiResponse.SCHEDULE_QUEUED)
                LOG.warn(String.format(
                        "StationID %s is queued server side and will be downloaded on next EPG update!",
                        o.getString("stationID")));
            else
                throw new InvalidJsonObjectException("Error received for schedule", o.toString(3));
        }
    } catch (JSONException | JsonParseException e) {
        Grabber.failedTask = true;
        LOG.fatal("Fatal JSON error!", e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        Grabber.failedTask = true;
        LOG.error("IOError receiving schedule data! Filling cache with empty schedules!", e);
        try {
            JSONArray schedIds = this.req;
            for (int i = 0; i < schedIds.length(); ++i) {
                String id = schedIds.getString(i);
                Path p = vfs.getPath("schedules", String.format("%s.txt", id));
                if (!Files.exists(p)) {
                    JSONObject emptySched = new JSONObject();
                    emptySched.put("stationID", id);
                    emptySched.put("programs", new JSONArray());
                    Files.write(p, emptySched.toString(3).getBytes(ZipEpgClient.ZIP_CHARSET));
                }
            }
        } catch (Exception x) {
            LOG.error("Unexpected error!", x);
            throw new RuntimeException(x);
        }
    }
}