【问题标题】:Retrofit2 Map<Object, String> call incompatible types errorRetrofit2 Map<Object, String> 调用不兼容类型错误
【发布时间】:2018-12-19 13:48:13
【问题描述】:

我正在尝试使用 Retrofit 2 进行 JSON 解析。我必须为我的项目进行地图调用。这是我的代码:

    retrofit2.Call<Map<String, Channel>> call =restInterface.getChannels();
    call.enqueue(new Callback<Map<String, Channel>>() {

        @Override
        public void onResponse(Response<Map<String, Channel>> response) {
            Map<String, Channel> body = response.body();
            for (Channel channel : body.values()) {
                System.out.println(channel.getSong());
            }
        }

        @Override
        public void onFailure(Throwable t) {
            // TODO
        }
    });

我收到不兼容的类型错误,Android Studio 建议转到此处:

retrofit2.Call<Map<String, Channel>> call =restInterface.getChannels();

到这里:

retrofit2.Call<List<Channel>> call =restInterface.getChannels();

我知道这是因为 Retrofit 的原始类型,但对于我的项目,我必须执行此调用。在这里你可以看到我的 JSON 在 API 中的样子:

{  
   "channel0":{  
      "song":"Cry Cry",
      "artist":"Oceana",
      "duration":"180",
      "playedat":"1545158211",
      "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/550513f3aaa5b80648972e715af345e5.png"
   },
   "channel1":{  
      "song":"Phantom Of The Opera",
      "artist":"Iron Maiden",
      "duration":"420",
      "playedat":"1545158002",
      "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/b2cf018fb80147428b698452b2512996.png"
   },
   "channel2":{  
      "song":"Carmen Cubana",
      "artist":"Klazz Brothers",
      "duration":"180",
      "playedat":"1545158161",
      "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/d518730378364a72ab98460bd0fe3d1a.png"
   },
   "channel3":{  
      "song":"Shut Up And Let Me Go",
      "artist":"The Ting Tings",
      "duration":"120",
      "playedat":"1545158234",
      "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/c5805b786a3e4f19b747c7a3dbb41d15.png"
   },
   "channel4":{  
      "song":"The Racing Rats",
      "artist":"Editors",
      "duration":"240",
      "playedat":"1545158095",
      "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/62544562d1904884b6e47804cf1ede1d.png"
   }
}

那么,我该怎么做呢?我对这个话题了解不多,所以我完全愿意接受针对我的问题的建议或示例。感谢您的帮助。

【问题讨论】:

  • .Call 的两个示例是相同的。这是故意的吗?此外,将您收到的确切错误消息显示为文本。
  • 我们也没有看到您定义的、您尝试调用的 API。 stackoverflow.com/a/33428445/1531971 相关吗?
  • @jdv 更正了它,我还添加了我的 JSON 示例。谢谢你的提醒。另外,我检查了你的链接。我认为这与我得到的情况无关。
  • 正如其中一个答案所提到的,您仍然没有显示您的 API 界面。

标签: java android retrofit2


【解决方案1】:

这是您如何定义 Retrofit 服务接口的问题。在某个地方你应该有一个看起来像这样的interface

public interface MyRetrofitService {

    @GET("/some/url")
    Call<List<Channel>> getChannels();
}

您必须将其更改为返回 Map&lt;String, Channel&gt; 而不是列表:

public interface MyRetrofitService {

    @GET("/some/url")
    Call<Map<String, Channel>> getChannels();
}

【讨论】:

    猜你喜欢
    • 2018-03-11
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    相关资源
    最近更新 更多