java在改造中,为什么每个接口使用一个方法
我是安卓和REST客户端的新手。我正在使用改型来构建一个应用程序来与API通信
许多教程说,最佳实践是为每个接口声明一个方法(如此多的方法意味着许多接口)
示例:如果我想要2个方法,一个GET和一个POST。我需要两个接口:
public interface GetService {
@GET("/abc/xyz")
Call <ABC> getService();
}
public interface PostService {
@POST("/abc/def")
Call<XYZ> postServer(@Body XYZ content);
}
在主要活动中,我需要打电话
//call get
GetService get = ServiceGenerator.createService(GetService.class);
ABC call1 = get.getService();
//call post
PostService post = ServiceGenerator.createService(PostService.class);
XYZ call2 = post.postService();
为什么我不能只有一个这样的界面:
public interface APIInterface {
@GET("/abc/xyz")
Call <ABC> getService();
@POST("/abc/def")
Call<XYZ> postServer(@Body XYZ content);
}
在主要的活动中,我只需要做以下几点:
APIInterface api = ServiceGenerator.createService(APIInterface.class);
ABC call1 = api.getService();
XYZ call2 = api.postServer();
# 1 楼答案
将所有内容放在一个界面中。无论你在哪里都可以轻松地称呼它。如果你有不同的基本Url,那么你可以有不同的接口。取决于开发者
# 2 楼答案
您不必遵循每个接口一个服务的“良好实践”。如果您在每个业务部分中使用一个界面,如下面所示,就可以了
这样做的目的只是为了避免与应用程序的所有服务建立接口