我正在使用Retrofit与我的REST API进行交互,并且想知道是否有人有任何设计建议.
我的应用程序包含以下包:
该服务包中包含用于改造的接口.例如:
public interface FooService {
@FormUrlEncoded
@POST("foo/do")
@Headers("Content-Type: application/x-www-form-urlencoded; charset=UTF-8")
Call<FooBar> do();
}
Run Code Online (Sandbox Code Playgroud)
模型包含......好吧,不同的模型.例如,FooBar.到目前为止一切都很好 - 就像Retrofit文档一样.
我创建了一个API类,它处理Retrofit构建逻辑(Retrofit retrofit = new Retrofit.Builder()等),并公开一个静态字段:retrofit.在我的活动中,我可以按如下方式执行我的请求:
FooService service = API.retrofit.create(FooService.class);
Call<FooBar> call = service.do();
try {
retrofit2.Response response = call.execute();
// ...do stuff...
} catch(IOException) {}
Run Code Online (Sandbox Code Playgroud)
随之而来的是我的问题:进一步抽象上述内容会更好吗?所以我不需要在任何地方重复上述内容?例如,类似于:
MyOtherFooService service = new MyOtherFooService();
FooBar fooBar = service.do();
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?建议?