在 Android 应用程序中显示 Deezer 用户播放列表

Displaying Deezer user playlists in Android app

我正在开发我在 github 上找到的一个小型 Android 播放器。

我设法编译了代码,但我使用的是 0.10.16 SDK。 github 上的播放器似乎是为以前的版本编写的。

我可以登录,但是当我在主屏幕上单击“播放列表”并执行以下代码时,出现空白屏幕:

   private void getUserPlaylists() {
    DeezerRequest request = DeezerRequestFactory.requestCurrentUserPlaylists();
    AsyncDeezerTask task = new AsyncDeezerTask(mDeezerConnect,
            new JsonRequestListener() {

                @SuppressWarnings("unchecked")
                @Override
                public void onResult(final Object result, final Object requestId) {

                    mPlaylistList.clear();

                    try {
                        mPlaylistList.addAll((List<Playlist>) result);
                    }
                    catch (ClassCastException e) {
                        handleError(e);
                    }

                    if (mPlaylistList.isEmpty()) {
                        Toast.makeText(UserPlaylistsActivity.this, getResources()
                                .getString(R.string.no_results), Toast.LENGTH_LONG).show();
                    }

                    mPlaylistAdapter.notifyDataSetChanged();
                }

                @Override
                public void onComplete(final String response, Object requestId) {
                    //TODO
                    Toast.makeText(UserPlaylistsActivity.this, "Playlist_onComplete",
                            Toast.LENGTH_LONG).show();


                }


                @Override
                public void onUnparsedResult(final String response, Object requestId) {
                    //TODO
                }


                @Override
                public void onException(Exception exception, Object requestId) {


                    if(exception instanceof OAuthException){
                        handleError(exception);
                    }
                    else if(exception instanceof MalformedURLException){
                        handleError(exception);
                    }
                    else if(exception instanceof IOException){
                        handleError(exception);
                    }
                    else if(exception instanceof DeezerError){
                        handleError(exception);
                    }
                    else if(exception instanceof JSONException){
                        handleError(exception);
                    }
                    else{
                        //do nothing
                    }
                }


            });
    task.execute(request);
}

我认为原因是,上面的代码是为以前的 SDK 版本编写的,显然适用于 "onResult"。然而,最新的 SDK 使用 "onComplete",returns 未解析的 JSON 字符串。

我的问题是:

我正在查看文档,但没有找到任何有用的信息。

有人用最新的 SDK 实现了吗?

编辑:

private void getUserPlaylists() {
    DeezerRequest request = DeezerRequestFactory.requestCurrentUserPlaylists();
    AsyncDeezerTask task = new AsyncDeezerTask(mDeezerConnect,
            new JsonRequestListener() {

                @SuppressWarnings("unchecked")
                @Override
                public void onResult(final Object result, final Object requestId) {

                    mPlaylistList.clear();

                    try {
                        mPlaylistList.addAll((List<Playlist>) result);
                    }
                    catch (ClassCastException e) {
                        handleError(e);
                    }

                    if (mPlaylistList.isEmpty()) {
                        Toast.makeText(UserPlaylistsActivity.this, getResources()
                                .getString(R.string.no_results), Toast.LENGTH_LONG).show();
                    }

                    mPlaylistAdapter.notifyDataSetChanged();
                }



                @Override
                public void onUnparsedResult(final String response, Object requestId) {
                    //TODO
                }


                @Override
                public void onException(Exception exception, Object requestId) {


                    if(exception instanceof OAuthException){
                        handleError(exception);
                    }
                    else if(exception instanceof MalformedURLException){
                        handleError(exception);
                    }
                    else if(exception instanceof IOException){
                        handleError(exception);
                    }
                    else if(exception instanceof DeezerError){
                        handleError(exception);
                    }
                    else if(exception instanceof JSONException){
                        handleError(exception);
                    }
                    else{
                        //do nothing
                    }
                }


            });
    task.execute(request);
}

这现在适用于 0.10.16 SDK。删除了 onComplete() 并且所有数据现在都被正确解析。菜单OK,播放成功

问题是您覆盖了 onComplete(String, Object) 方法。此方法已存在于 JsonRequestListener 实现中,因此您不应该自己重写它,至少在不调用 super.onComplete(response, requestId).

的情况下不能。

当重写 JsonResultListener class 时,您应该只像以前那样实现 onResult(Object, Object)onUnparsedResult(String, Object) 方法以防 json 不能' t 会被自动解析,并且 onException(Exception, Object) 以防出现异常。