首页 > 解决方案 > Retrofit 2.0 + GSON + Android: Unable to invoke no-args constructor for interface retrofit2.http.Body

问题描述

I am trying to create a simple News app with the following endpoint:

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

I know there are 2-3 questions already on SO about this but I belive that this exception is unique since it can not create a constructor for a library interace?!...

Here are my project files:

Retrofit client

Model classes

Custom adapter and activity

I get the following RuntimeException when trying to call

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

In the main activity:

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

So far I have tried:

Any idea what could cause this? I belive my code should be working and since the error is with a the retrofit2.http.Body interface which to my knowledge I should not have to implement anywhere...

I really appreciate any form of help! Thanks!

标签: javaandroidjsongsonretrofit

解决方案


您拥有的Blocks模型没有导入正确的Body类型。

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

import retrofit2.http.Body;

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


推荐阅读