Esempio n. 1
0
 private void changeHeaderForRequestHttps(ArrayList<Team> teams) {
   for (Team team : teams) {
     String thumbnail = team.getThumbnail();
     if (!thumbnail.contains("https")) {
       team.setThumbnail(thumbnail.replace("http", "https"));
     }
     if (thumbnail.startsWith("upload")) {
       team.setThumbnail("https://" + thumbnail);
     }
   }
 }
Esempio n. 2
0
  /**
   * Gets the teams provided by the service and persist them.
   *
   * @param receiver Receiver object which is used to send data about download progress of teams.
   */
  private void getAllTeamsAndPersistThem(ResultReceiver receiver) {
    Log.d(LOG_TAG, "Reached getAllTeamsAndPersistThem");
    DaoHelper daoHelper = new DaoHelper(this);

    if (daoHelper.getTeamCount() == 226) {
      activeSpinner(SPINNER_NOT_ACTIVE);
      return;
    }

    final ArrayList<String> leagues = new ArrayList<>();
    leagues.add("394");
    leagues.add("395");
    leagues.add("396");
    leagues.add("397");
    leagues.add("398");
    leagues.add("399");
    leagues.add("400");
    leagues.add("401");
    leagues.add("402");
    leagues.add("403");
    leagues.add("404");
    leagues.add("405");

    ArrayList<Team> teams = new ArrayList<>();

    for (String league : leagues) {
      try {
        URL url = createSeasonTeamUrl(league);
        String result = RestClient.getData(url.toString(), getString(R.string.api_key));

        ArrayList<Team> teamsTemp = Parser.parseTeams(result);
        teams.addAll(teamsTemp);

      } catch (MalformedURLException urlEx) {
        Log.e(LOG_TAG, "Error ", urlEx);
      } catch (IOException e) {
        Log.e(LOG_TAG, "Error ", e);
      }
    }

    // workaround - change http header per https
    changeHeaderForRequestHttps(teams);

    Vector<ContentValues> values = new Vector<>(leagues.size());
    int load = 0;
    for (Team team : teams) {

      if (null != team) {
        ContentValues teamValues = new ContentValues();
        teamValues.put(DatabaseContract.TeamEntry._ID, team.getId());
        teamValues.put(DatabaseContract.TeamEntry.NAME, team.getName());
        teamValues.put(DatabaseContract.TeamEntry.SHORT_NAME, team.getShortName());
        teamValues.put(DatabaseContract.TeamEntry.THUMBNAIL, team.getThumbnail());
        values.add(teamValues);
        Log.d("item downloaded", String.valueOf(team.getId()));
      }

      // data that will be send into ResultReceiver
      Bundle data = new Bundle();
      data.putInt(PROGRESS, ++load);

      // sends progress into ResultReceiver located in your Activity
      receiver.send(NEW_PROGRESS, data);
    }

    // activeSpinner(SPINNER_ACTIVE);
    insertData(values);
    activeSpinner(SPINNER_NOT_ACTIVE);
  }