java.lang.IllegalArgumentException:API接口不得扩展其他接口Retrofit 2 [英] java.lang.IllegalArgumentException: API interfaces must not extend other interfaces Retrofit 2
问题描述
我在使用Retrofit 2 beta 2时遇到下一个问题:
I am having the next problem using Retrofit 2 beta 2:
java.lang.IllegalArgumentException: API interfaces must not extend other interfaces.
这是因为我有一个这样的Retrofit API接口:
This is because I have one interface for the API of Retrofit like this:
public interface RetrofitBaseAPI {
@POST
Call<LoginResp> login(@Url String url, @Body LoginReq loginReq);
@POST
Call<String> logout(@Url String url, @Header("Cookie") String sid);
}
例如,其中之一就是这个:
For example, one of them is this one:
public interface RetrofitWiserLinkAPI extends RetrofitBaseAPI {
@GET("/rs/DeviceIdentification")
Call<DeviceId> getDeviceIdentification(@Header("Cookie") String sid);
}
然后,我还有其他三个接口,其中三个是从RetrofitBaseAPI接口扩展的.
And then, I have three other interfaces, the three of them extends from this RetrofitBaseAPI interface.
当我尝试使用给定的接口调用retrofit.create(Class class)时,我总是会收到此错误.
When I try to call the retrofit.create(Class class) with the given interface, I always receive this error.
据我所读,唯一的解决方案是创建三个独立接口.是真的吗有人知道另一种解决方案吗?
As far as I was reading, the only solution is to create three independents interfaces. Is it true? Anybody knows another solution?
我觉得有点奇怪,我们需要复制代码,但是,也许是有一个我不理解的原因.....
I find a little bit weird that we need to duplicate code, but well, maybe there is a reason I don't understand.....
提前谢谢!
谢谢
使用最终的Retrofit 2发行版时出现相同的问题.我想这是Retrofit的一个局限....
Same problem using the final Retrofit 2 release version. I guess it is a limitation from Retrofit....
推荐答案
不可能有基本的Retrofit接口.
It's not possible to have a base Retrofit interface.
改造比继承更偏向于组合.每个服务一个接口.
Retrofit favors composition over inheritance. One interface per service.
因此,正如您已经发现的那样,唯一的解决方案是创建三个独立接口.
So as you already found out, the only solution is to create three independents interfaces.
这篇关于java.lang.IllegalArgumentException:API接口不得扩展其他接口Retrofit 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!