Exemplo n.º 1
0
    @Override
    public Playlist doInBackground(Void... params) {
      JamendoGet2Api server = new JamendoGet2ApiImpl();
      int[] id = null;
      try {
        id = server.getTop100Listened();
        // if loading rss failed and no musics are there - report an error
        if (id == null) {
          publishProgress(new WSError((String) getResources().getText(R.string.top100_fail)));
          return null;
        }
        Album[] albums = server.getAlbumsByTracksId(id);
        Music[] musics =
            server.getTracksByTracksId(id, Dian1Application.getInstance().getStreamEncoding());
        if (albums == null || musics == null) return null;
        Hashtable<Integer, PlaylistEntry> hashtable = new Hashtable<Integer, PlaylistEntry>();
        for (int i = 0; i < musics.length; i++) {
          PlaylistEntry playlistEntry = new PlaylistEntry();
          playlistEntry.setAlbum(albums[i]);
          playlistEntry.setMusic(musics[i]);
          hashtable.put(musics[i].getId(), playlistEntry);
        }

        // creating playlist in the correct order
        Playlist playlist = new Playlist();
        for (int i = 0; i < id.length; i++) {
          playlist.addPlaylistEntry(hashtable.get(id[i]));
        }
        return playlist;
      } catch (JSONException e) {
        e.printStackTrace();
      } catch (WSError e) {
        publishProgress(e);
      }
      return null;
    }