提问者:小点点

Retrofit2 Android:检索嵌套数据(当前错误:预期BEGIN_ARRAY但在第1行第2列路径$处BEGIN_OBJECT)


我正试图从漫威API中检索一些角色数据,它看起来像这样:

{
"code": 200,
"status": "Ok",
"copyright": "© 2019 MARVEL",
"attributionText": "Data provided by Marvel. © 2019 MARVEL",
"attributionHTML": "<a href=\"http://marvel.com\">Data provided by Marvel. © 2019 MARVEL</a>",
"etag": "cc6c6c3e7f5e336ecf899ab5652d55e6a3a51b09",
"data": 

{
    "offset": 0,
    "limit": 20,
    "total": 1493,
    "count": 20,
    "results": 

[
        {
        "id": 1011334,
        "name": "3-D Man",
        "description": "",
        "modified": "2014-04-29T14:18:17-0400",
        "thumbnail": {
        "path": "http://i.annihil.us/u/prod/marvel/i/mg/c/e0/535fecbbb9784",
        "extension": "jpg"
        },
        "resourceURI": "http://gateway.marvel.com/v1/public/characters/1011334",
        "comics": {},
        "series": {},
        "stories": {},
        "events": {},
        "urls": []
        },

但我显然只想展示结果(数据-

textViewResult = findViewById(R.id.textView_result);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://gateway.marvel.com/v1/public/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MarvelAPI marvelAPI = retrofit.create(MarvelAPI.class);

        Call<List<Character>> call = marvelAPI.getCharacters();

        call.enqueue(new Callback<List<Character>>() {
            @Override
            public void onResponse(Call<List<Character>> call, Response<List<Character>> response) {
                if(!response.isSuccessful()){
                    textViewResult.setText("Code: " + response.code());
                    return;
                }
                List<Character> characters = response.body();

                for (Character character : characters){
                    String content = "";
                    content += "ID: " + character.getId() + "\n";
                    content += "Name: " + character.getName() + "\n";
                    content += "Description: " + character.getDescription() + "\n";
                    content += "Modified: " + character.getModified() + "\n";
                    content += "Thumbnail: " + character.getThumbnail() + "\n";
                    textViewResult.append(content);
                }
            }

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

活动中使用的MarvelAPI接口:

public interface MarvelAPI {

    @GET("characters")
    Call<List<Character>> getCharacters();

}

这显然行不通,因为它给我一个“预期的BEGIN_ARRAY,但在第1行第2列path $”错误。我还尝试创建了两个新类:包含结果列表的CharacterData和包含字符列表的CharactersResults,但这也不起作用(即使在尝试更改call.enqueue和MarvelAPI接口之后)..或者我真的不知道如何改变我的调用。enqueue让它用这3个类获取正确的数据。有人能告诉我正确的方向吗?

(附言:我确实知道把所有结果放在文本视图中很混乱,但这只是API检索正确数据的第一个版本,我确实计划把它放在网格或其他地方)


共1个答案

匿名用户

处理这个问题的最简单方法实际上是使用您提到的模型。本质上,让java对象模仿json结构。在这种情况下,您可以执行以下操作:

class CharacterData {
  @SerializedName("results")
  List<Character> characters;
  // ...
}

class CharactersResults {
  @SerializedName("data")
  CharacterData data;
  // ...
}

就像你说的,现在你需要改变改装电话。代理的接口现在必须返回<code>CharactersResults</code>:

public interface MarvelAPI {
  @GET("characters")
  Call<CharactersResults> getCharacters();
}

这又会改变回调参数:

call.enqueue(new Callback<CharactersResults>() {
   @Override
   public void onResponse(Call<CharactersResults> call, Response<CharactersResults> response) {
      // ...

      List<Character> characters = response.body().data.characters;

      // ...
   }

   @Override
   public void onFailure(Call<CharactersResults> call, Throwable t) {
      // ...
   }
});

PS:为了简洁,我忽略了创建getters和setters