将Retrofit服务声明划分为多个接口 [英] Divide Retrofit service declaration into multiple interfaces

查看:95
本文介绍了将Retrofit服务声明划分为多个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个链接到API的应用程序,其中包含大约265种方法。我非常希望将这些API的声明分解为多个文件,以保持它们的有序性和可访问性。但是,Retrofit明确禁止通过扩展组合接口。

I am creating an app linking to an API with about 265 methods to it. I would very much like to break apart the declaration of these APIs into multiple files to keep them organized and accessible. However Retrofit explicitly disallows combining interfaces through extension.

java.lang.IllegalArgumentException: Interface definitions must not extend other interfaces.

我一直试图将其声明如下。

I had been trying to declare it as follows.

public interface ApiService extends ProfileService, AccountService {
    // Empty interface, methods divided into other services
}

public interface ProfileService {
    @GET("/api/v1/protected/profile")
    public void getProfile(Callback<Profile> callback);

    ...
}

public interface AccountService {
    @GET("/api/v1/protected/account")
    public void getAccount(Callback<Account> callback);


    ...
}

那里是关于拉取请求的这个问题的讨论。图书馆作者已经决定扩展这样的接口不符合图书馆的精神。
https://github.com/square/retrofit/pull/676

There is a discussion about this issue on a pull request. The library authors have decided that extending interfaces like this is not in the spirit of the library. https://github.com/square/retrofit/pull/676

Jake Wharton(作者)说改造有利于构图。并且回应你真的有一个带有大量代理的单个适配器吗?,是的。它们是从protos中的服务声明生成的。每个服务一个接口。

Jake Wharton (the author) says that "Retrofit favors composition." And in response to "Do you really have a single adapter with a ton of proxies?", "Yes. They are generated from a service declaration in protos. One interface per service."

我一直在阅读有关组合与继承的内容,并且未能掌握如何实现打破声明的目标。

I've been reading about composition vs inheritance and fail to grasp how to achieve the goal of breaking up the declaration.

如何划分界面声明吗?我缺少最佳做法吗?

How can I divide the interface declaration up? Is there a best practice that I'm missing?

谢谢。

推荐答案

只需创建单独的界面。

public interface ProfileService {

  /* ... */
}

public interface AccountService {

  /* ... */
}

ProfileService profileService = mRestAdapter.create(ProfileService.class);
AccountService accountService = mRestAdapter.create(AccountService.class);

这篇关于将Retrofit服务声明划分为多个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆