@Override public void onSuccess(JSONObject response) { JSONArray array = response.optJSONArray("list"); List<User> usersToInsert = parseUsers(array); for (int i = 0; i < usersToInsert.size(); i++) { KidsLogger.d( LOG_TAG, String.format("Getting user : %s ", usersToInsert.get(i).getScreenName())); if (usersToInsert.get(i).getScreenName().equals("null")) { KidsLogger.d( LOG_TAG, String.format("Removing user : %s ", usersToInsert.get(i).getScreenName())); usersToInsert.remove(i); } } if (!insertNewUsers(usersToInsert)) { mCallback.onFailure(new IOException("Impossible to insert users in db.")); return; } if (response.optBoolean("has_more") && response.optInt("page") != 0) { fetchPage(response.optInt("page") + 1); } else { deleteOutdatedUsers(); mCallback.onSuccess(mFetchedUsers); } }
/** * Fetch the provided page of users for the Dailymotion end point generated based on the object we * need to fetch the users for. * * @param page Page to fetch as an int. */ private void fetchPage(int page) { KidsLogger.d(LOG_TAG, String.format("Fetching users from %s, page : %d", mEndPoint, page)); Bundle bundle = new Bundle(); bundle.putString("fields", FIELDS); bundle.putString("tileset", "kidsplus"); bundle.putString("localization", "fr"); bundle.putString("page", String.valueOf(page)); bundle.putString("limit", String.valueOf(Constants.PAGE_LIMIT)); DailymotionRequestor requestor = new DailymotionRequestor(mDailymotion); requestor.anonymousRequest(mEndPoint, bundle, new UserFetchingRequestListener()); }
/** * Insert the provided playlists in the local database. A boolean is returned to indicate if the * insertion went correctly. */ private boolean insertNewUsers(List<User> users) { if (users.size() > 0) { KidsLogger.d(LOG_TAG, String.format("Inserting %d users", users.size())); ContentValues[] values = new ContentValues[users.size()]; for (int i = 0; i < users.size(); ++i) { values[i] = users.get(i).toContentValues(); } return (mContentResolver.bulkInsert(UsersProvider.CONTENT_URI, values) == users.size()); } return true; }