首页 > 解决方案 > API 返回 JSONObjects 而不是 Retrofit + Android 上的数组

问题描述

我目前正在尝试在 Android 上使用 Retrofit 实现 Spotify API 端点,我已经处理这个问题一段时间了。与大多数 API 不同,Spotify 的 JSON 响应只是一个 JSONObject,其他嵌套在其中。当然,Retrofit 给了我一个“预期的 BEGIN_ARRAY 但是 BEGIN_OBJECT”的错误。有谁知道解决方案?以下是一些相关代码:

OnActivityResult(在 Spotify 身份验证后)

protected void onActivityResult(final int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        // Check if result comes from the correct activity
        if (requestCode == REQUEST_CODE) {
            final AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);

            switch (response.getType()) {
                // Response was successful and contains auth token
                case TOKEN:
                    // Handle successful response
                    SpotifyApi api = new SpotifyApi();

                    api.setAccessToken(response.getAccessToken());

                    SpotifyService spotify = api.getService();

                    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request newRequest  = chain.request().newBuilder()
                                    .addHeader("Authorization", "Bearer " + response.getAccessToken())
                                    .build();
                            return chain.proceed(newRequest);
                        }
                    }).build();

                    Retrofit retrofit = new Retrofit.Builder()
                            .client(client)
                            .baseUrl("https://api.spotify.com/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();

                    SpotifyPlayerApi spotifyPlayerApi = retrofit.create(SpotifyPlayerApi.class);

                    Call<List<CurrentlyPlaying>> call = spotifyPlayerApi.getCurrentlyPlaying();
                    call.enqueue(new Callback<List<CurrentlyPlaying>>() {
                        @Override
                        public void onResponse(Call<List<CurrentlyPlaying>> call, retrofit2.Response<List<CurrentlyPlaying>> response) {
                            if(!response.isSuccessful())
                            {
                                testResult.setText("Code: " + response.code() + ", " + response.message());
                                return;
                            }

                            for(CurrentlyPlaying currentlyPlaying : response.body())
                            {
                                testResult.setText(currentlyPlaying.getImageUrl());
                            }
                        }

                        @Override
                        public void onFailure(Call<List<CurrentlyPlaying>> call, Throwable t) {
                            testResult.setText(t.getMessage());
                        }
                    });


                    //List<Tracks> recommendations = spotify.getRecommendations(//).tracks;
                    /** ^ Parameter 'Map' for 'getRecommendations' = String: "seed_track", Object: "TRACK ID" (whatever's currently playing) **/
                    break;

                // Auth flow returned an error
                case ERROR:
                    // Handle error response
                    break;

                // Most likely auth flow was cancelled
                default:
                    // Handle other cases
            }
        }

所需数据类型的类

public class CurrentlyPlaying {
    @SerializedName("/item/id")
    private String songId;

    @SerializedName("/item/images/url")
    private String imageUrl;

    public String getSongId() {
        return songId;
    }

    public String getImageUrl() {
        return imageUrl;
    }
}

API接口

public interface SpotifyPlayerApi {
    @GET("v1/me/player/currently-playing")
    Call<List<CurrentlyPlaying>> getCurrentlyPlaying();
}

任何帮助将不胜感激。

标签: androidjsonretrofitspotify

解决方案


修改你的界面看起来像..

 public interface SpotifyPlayerApi {
 @GET("v1/me/player/currently-playing")
 Call<APiResponse> getCurrentlyPlaying();
 }

并添加以下模型类,希望它能解决您的问题。要创建 pojo,我使用,您可以自己尝试。

public class APiResponse{
private String currently_playing_type;

private Item item;

private Context context;

private String is_playing;

private String progress_ms;

private String timestamp;

public String getCurrently_playing_type ()
{
    return currently_playing_type;
}

public void setCurrently_playing_type (String currently_playing_type)
{
    this.currently_playing_type = currently_playing_type;
}

public Item getItem ()
{
    return item;
}

public void setItem (Item item)
{
    this.item = item;
}

public Context getContext ()
{
    return context;
}

public void setContext (Context context)
{
    this.context = context;
}

public String getIs_playing ()
{
    return is_playing;
}

public void setIs_playing (String is_playing)
{
    this.is_playing = is_playing;
}

public String getProgress_ms ()
{
    return progress_ms;
}

public void setProgress_ms (String progress_ms)
{
    this.progress_ms = progress_ms;
}

public String getTimestamp ()
{
    return timestamp;
}

public void setTimestamp (String timestamp){
    this.timestamp = timestamp;
 }
}

项目型号

public class Item{
private String disc_number;

private Album album;

private String[] available_markets;

private String type;

private External_ids external_ids;

private String uri;

private String duration_ms;

private String explicit;

private Artists[] artists;

private String preview_url;

private String popularity;

private String name;

private String track_number;

private String href;

private String id;

private External_urls external_urls;

public String getDisc_number ()
{
    return disc_number;
}

public void setDisc_number (String disc_number)
{
    this.disc_number = disc_number;
}

public Album getAlbum ()
{
    return album;
}

public void setAlbum (Album album)
{
    this.album = album;
}

public String[] getAvailable_markets ()
{
    return available_markets;
}

public void setAvailable_markets (String[] available_markets)
{
    this.available_markets = available_markets;
}

public String getType ()
{
    return type;
}

public void setType (String type)
{
    this.type = type;
}

public External_ids getExternal_ids ()
{
    return external_ids;
}

public void setExternal_ids (External_ids external_ids)
{
    this.external_ids = external_ids;
}

public String getUri ()
{
    return uri;
}

public void setUri (String uri)
{
    this.uri = uri;
}

public String getDuration_ms ()
{
    return duration_ms;
}

public void setDuration_ms (String duration_ms)
{
    this.duration_ms = duration_ms;
}

public String getExplicit ()
{
    return explicit;
}

public void setExplicit (String explicit)
{
    this.explicit = explicit;
}

public Artists[] getArtists ()
{
    return artists;
}

public void setArtists (Artists[] artists)
{
    this.artists = artists;
}

public String getPreview_url ()
{
    return preview_url;
}

public void setPreview_url (String preview_url)
{
    this.preview_url = preview_url;
}

public String getPopularity ()
{
    return popularity;
}

public void setPopularity (String popularity)
{
    this.popularity = popularity;
}

public String getName ()
{
    return name;
}

public void setName (String name)
{
    this.name = name;
}

public String getTrack_number ()
{
    return track_number;
}

public void setTrack_number (String track_number)
{
    this.track_number = track_number;
}

public String getHref ()
{
    return href;
}

public void setHref (String href)
{
    this.href = href;
}

public String getId ()
{
    return id;
}

public void setId (String id)
{
    this.id = id;
}

public External_urls getExternal_urls ()
{
    return external_urls;
}

public void setExternal_urls (External_urls external_urls)
{
    this.external_urls = external_urls;
}
}

External_urls 模型

public class External_urls{
private String spotify;

public String getSpotify ()
{
    return spotify;
}

public void setSpotify (String spotify)
{
    this.spotify = spotify;
}

}

External_ids 模型

public class External_ids{
private String isrc;

public String getIsrc ()
{
    return isrc;
}

public void setIsrc (String isrc)
{
    this.isrc = isrc;
}

}

艺术家模型

public class Artists{
private String name;

private String href;

private String id;

private String type;

private External_urls external_urls;

private String uri;

public String getName ()
{
    return name;
}

public void setName (String name)
{
    this.name = name;
}

public String getHref ()
{
    return href;
}

public void setHref (String href)
{
    this.href = href;
}

public String getId ()
{
    return id;
}

public void setId (String id)
{
    this.id = id;
}

public String getType ()
{
    return type;
}

public void setType (String type)
{
    this.type = type;
}

public External_urls getExternal_urls ()
{
    return external_urls;
}

public void setExternal_urls (External_urls external_urls)
{
    this.external_urls = external_urls;
}

public String getUri ()
{
    return uri;
}

public void setUri (String uri)
{
    this.uri = uri;
}
}

专辑型号

public class Album{
private Images[] images;

private String name;

private String album_type;

private String href;

private String id;

private String type;

private External_urls external_urls;

private String uri;

public Images[] getImages ()
{
    return images;
}

public void setImages (Images[] images)
{
    this.images = images;
}

public String getName ()
{
    return name;
}

public void setName (String name)
{
    this.name = name;
}

public String getAlbum_type ()
{
    return album_type;
}

public void setAlbum_type (String album_type)
{
    this.album_type = album_type;
}

public String getHref ()
{
    return href;
}

public void setHref (String href)
{
    this.href = href;
}

public String getId ()
{
    return id;
}

public void setId (String id)
{
    this.id = id;
}

public String getType ()
{
    return type;
}

public void setType (String type)
{
    this.type = type;
}

public External_urls getExternal_urls ()
{
    return external_urls;
}

public void setExternal_urls (External_urls external_urls)
{
    this.external_urls = external_urls;
}

public String getUri ()
{
    return uri;
}

public void setUri (String uri)
{
    this.uri = uri;
}

}

图像模型

public class Images{
private String width;

private String url;

private String height;

public String getWidth ()
{
    return width;
}

public void setWidth (String width)
{
    this.width = width;
}

public String getUrl ()
{
    return url;
}

public void setUrl (String url)
{
    this.url = url;
}

public String getHeight ()
{
    return height;
}

public void setHeight (String height)
{
    this.height = height;
}

}

上下文模型

public class Context{
private String href;

private String type;

private External_urls external_urls;

private String uri;

public String getHref ()
{
    return href;
}

public void setHref (String href)
{
    this.href = href;
}

public String getType ()
{
    return type;
}

public void setType (String type)
{
    this.type = type;
}

public External_urls getExternal_urls ()
{
    return external_urls;
}

public void setExternal_urls (External_urls external_urls)
{
    this.external_urls = external_urls;
}

public String getUri ()
{
    return uri;
}

public void setUri (String uri)
{
    this.uri = uri;
}
}

推荐阅读