例如:我喜欢使用库改造的下一个代码。我创建了抽象类并使用它来简化创建改进API接口实现。 我可以在Android Studio(或IDEA)中添加此代码,并在需要时使用自动生成吗?
public abstract class AnyApiBuilder<T> {
private static final int TIMEOUT = 20;
public abstract T build();
protected T build(Class<T> api) {
return build(api, ServerUri.MAIN_SERVER_URI);
}
protected T build(Class<T> api, String uri) {
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(TIMEOUT, TimeUnit.SECONDS);
return new RestAdapter.Builder()
.setEndpoint(uri)
.setClient(new OkClient(okHttpClient))
.setLogLevel(RestAdapter.LogLevel.FULL)
.build()
.create(api);
}
}
和
public interface GetAppsApi {
@POST("/get_apps")
void getApps(@Body GetAppsRequest request, Callback<GetAppsResponse> callback);
class Builder extends AnyApiBuilder<GetAppsApi> {
public GetAppsApi build() {
return build(GetAppsApi.class);
}
}
}