Retrofit 2.0 + GSON + Android:无法为接口Retrofit2.http.Body调用无参数构造函数

时间:2018-07-21 19:17:36

标签: java android json gson retrofit

我正在尝试创建一个具有以下端点的简单新闻应用程序:

https://content.guardianapis.com/search?api-key=...&show-fields=thumbnail&show-blocks=body&type=article

我知道关于此问题已有2-3个问题,但我相信此异常是唯一的,因为它无法为库接口创建构造函数?!...

这是我的项目文件:

Retrofit client

Model classes

Custom adapter and activity

我在尝试调用时收到以下RuntimeException

call.enqueue(new Callback<News>() {

在主要活动中:

Unable to invoke no-args constructor for interface retrofit2.http.Body. Registering an InstanceCreator with Gson for this type may fix this problem.

到目前为止,我已经尝试过:

  • 为我的每个模型类创建无参数构造函数,如另一个类似问题中所述

  • 检查是否有任何类通过herehere

  • 的改进接口共享相似的名称

有什么想法会导致这种情况吗?我相信我的代码应该可以正常工作,并且由于错误是由retrofit2.http.Body接口引起的,据我所知,我不必在任何地方实现……

我非常感谢任何形式的帮助!谢谢!

1 个答案:

答案 0 :(得分:0)

您使用的Blocks模型不是import正确的Body类型。

您所要做的就是删除以下行:

import retrofit2.http.Body;

这样,您的模型将使用同一包中的Body类。