java - Retrofit2 安卓 : Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

标签 java android json retrofit retrofit2

我知道这不是第一次有人询问这个问题,但是使用 Retrofit2 我找不到解决问题的正确方法。我按照在线教程进行操作,效果很好。当我将相同的代码应用于我自己的端点时,我得到了这个异常: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 我不知道如何解决这个问题。

界面:

public interface MyApiService {

// Is this right place to add these headers?
@Headers({"application-id: MY-APPLICATION-ID",
        "secret-key: MY-SECRET-KEY",
        "application-type: REST"})
@GET("Music")
Call<List<Music>> getMusicList();



Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(MySettings.REST_END_POINT)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
}

客户端代码:

MyApiService service = MyApiService.retrofit.create(MyApiService.class);
Call<List<Music>> call = service.getMusicList();
call.enqueue(new Callback<List<Music>>() {

    @Override
    public void onResponse(Call<List<Music>> call, Response<List<Music>> response) {
        Log.e("MainActivity", response.body().
    }

    @Override
    public void onFailure(Call<List<Music>> call, Throwable t) {
        Log.e("MainActivity", t.toString());
    }
});

此代码使用此有效负载:

[
{
    "login": "JakeWharton",
    "id": 66577,
    "avatar_url": "https://avatars.githubusercontent.com/u/66577?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/JakeWharton",
    "html_url": "https://github.com/JakeWharton",
    "followers_url": "https://api.github.com/users/JakeWharton/followers",
    "following_url": "https://api.github.com/users/JakeWharton/following{/other_user}",
    "gists_url": "https://api.github.com/users/JakeWharton/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/JakeWharton/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/JakeWharton/subscriptions",
    "organizations_url": "https://api.github.com/users/JakeWharton/orgs",
    "repos_url": "https://api.github.com/users/JakeWharton/repos",
    "events_url": "https://api.github.com/users/JakeWharton/events{/privacy}",
    "received_events_url": "https://api.github.com/users/JakeWharton/received_events",
    "type": "User",
    "site_admin": false,
    "contributions": 741
},
{....

但不是这个:

{
"offset": 0,
"data": [
    {
        "filename": "E743_1458662837071.mp3",
        "created": 1458662854000,
        "publicUrl": "https://api.backendless.com/dbb77803-1ab8-b994-ffd8-65470fa62b00/v1/files/music/E743_1458662837071.mp3",
        "___class": "Music",
        "description": "",
        "likeCount": 0,
        "title": "hej Susanne. ",
        "ownerId": "E743756F-E114-6892-FFE9-BCC8C072E800",
        "updated": null,
        "objectId": "DDD8CB3D-ED66-0D6F-FFA5-B14543ABC800",
        "__meta": "{\"relationRemovalIds\":{},\"selectedProperties\":[\"filename\",\"created\",\"publicUrl\",\"___class\",\"description\",\"likeCount\",\"title\",\"ownerId\",\"updated\",\"objectId\"],\"relatedObjects\":{}}"
    },
    {...

我的音乐课:

public class Music {

   private String ownerId;
   private String filename;
   private String title;
   private String description;
   private String publicUrl;
   private int likeCount;

   // Getters & Setters

}

最佳答案

当您说“此代码正在使用此有效负载:...但不适用于此:...”时,这是预期的,这就是它的工作方式。实际上,错误消息告诉您,在将 json 转换为 java 对象时,调用期望 json 中的数组但得到了一个对象。

这个电话:

@GET("Music")
Call<List<Music>> getMusicList();

需要 Music 对象的列表,这就是它与 json 一起使用的原因:

[
  {
    "login": "JakeWharton",
    ...
  },
  ...
]

因为 json 本身是 Music 对象的数组(Retrofit 可以在 json 数组之间转换为 java 列表)。对于第二个 json,您只有一个对象而不是数组(注意缺少 [...])。为此,您需要使用映射到该 json 的另一个模型创建另一个调用。假设您已将模型命名为 MusicList。下面是调用的样子:

@GET("Music")
Call<MusicList> getMusicList();

(请注意,如果您想同时保留第一次调用和这次调用,可能需要更改方法名称)。

MusicList 模型可能如下所示:

public class MusicList {
  @SerializedName("data")
  private List<Music> musics;
  // ...
}

我假设 data 数组是 Music 对象的列表,但我确实注意到 json 完全不同。您可能还需要调整它,但我想您已经了解了这里的情况。

关于java - Retrofit2 安卓 : Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36177629/

相关文章:

android - Android SELinux 中是否可以使用包名作为域名?

javascript - Discord bot 无法从附件加载数据

java - 关于Java中的String对象和按值传递

java - 当我说 Prepared 语句是预编译的是什么意思?

java - 设置者,为什么这不起作用?

java - 整个程序中的独特任务

android - LinkMovementMethod 导致 EditText 中的编辑问题

android - 使用 Android 模拟器进行远程调试

javascript - 将 json 值打印到 html 中

python 自定义 JSON 编码器/解码器未按预期工作