android – 如何使用Retrofit解决多个API端点?

android – 如何使用Retrofit解决多个API端点?,第1张

概述在我的 Android项目中,我使用以下 Retrofit ApiModule作为一个API端点.请注意,我使用 Dagger注入依赖项. @Module( complete = false, library = true)public final class ApiModule { public static final String PRODUCT 在我的 Android项目中,我使用以下 Retrofit APIModule作为一个API端点.请注意,我使用 Dagger注入依赖项.
@Module(        complete = false,library = true)public final class APIModule {    public static final String PRODUCTS_BASE_URL = "https://products.com";    @ProvIDes    @Singleton    Endpoint provIDeEndpoint() {        return Endpoints.newFixedEndpoint(PRODUCTS_BASE_URL);    }    @ProvIDes    @Singleton    ObjectMapper provIDeObjectMapper() {        ObjectMapper objectMapper = new ObjectMapper();        objectMapper.setPropertyNamingStrategy(            PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERscoreS);        return objectMapper;    }    @ProvIDes    @Singleton    RestAdapter provIDeRestAdapter(        Endpoint endpoint,ObjectMapper objectMapper) {        return new RestAdapter.Builder()                .setLogLevel(RestAdapter.LogLevel.NONE)                .setEndpoint(endpoint)                .setConverter(new JacksonConverter(objectMapper))                .build();    }    @ProvIDes    @Singleton    ProductsService provIDeProductsService(RestAdapter restAdapter) {        return restAdapter.create(ProductsService.class);    }}

现在,我想与之沟通另一个API(例如“http://subsIDiarIEs.com”).是否可以在重用ObjectMapper和RestAdapter时扩展给定的APIModule?或者我不应该延长它?我已经尝试复制模块了.但这涉及到我必须复制Endpoint,ObjectMapper和…… RestAdapter有一个私有的构造函数 – 所以我不能.

解决方法 我猜你可以使用named annotations:
@Module(        complete = false,library = true)public final class APIModule {    public static final String PRODUCTS_BASE_URL = "https://products.com";    public static final String SUBSIDIARIES_BASE_URL = "https://subsIDiarIEs.com";    public static final String PRODUCTS = "products";    public static final String SUBSIDIARIES = "subsIDiarIEs";    @ProvIDes    @Singleton    @named(PRODUCTS)    Endpoint provIDeProductsEndpoint() {        return Endpoints.newFixedEndpoint(PRODUCTS_BASE_URL);    }    @ProvIDes    @Singleton    @named(SUBSIDIARIES)    Endpoint provIDeSubsIDiarIEsEndpoint() {        return Endpoints.newFixedEndpoint(SUBSIDIARIES_BASE_URL);    }    @ProvIDes    @Singleton    ObjectMapper provIDeObjectMapper() {        ObjectMapper objectMapper = new ObjectMapper();        objectMapper.setPropertyNamingStrategy(            PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERscoreS);        return objectMapper;    }    @ProvIDes    @Singleton    @named(PRODUCTS)    RestAdapter provIDeProductsRestAdapter(@named(PRODUCTS) Endpoint endpoint,ObjectMapper objectMapper) {        return newRestAdapterBuilder(objectMapper)                .setEndpoint(endpoint)                .build();    }    @ProvIDes    @Singleton    @named(SUBSIDIARIES)    RestAdapter provIDeSubsIDiarIEsRestAdapter(@named(SUBSIDIARIES) Endpoint endpoint,ObjectMapper objectMapper) {        return newRestAdapterBuilder(objectMapper)                .setEndpoint(endpoint)                .build();    }    @ProvIDes    @Singleton    @named(PRODUCTS)    ProductsService provIDeProductsService(@named(PRODUCTS) RestAdapter restAdapter) {        return restAdapter.create(ProductsService.class);    }    @ProvIDes    @Singleton    @named(SUBSIDIARIES)    ProductsService provIDeSubsIDiarIEsService(@named(SUBSIDIARIES) RestAdapter restAdapter) {        return restAdapter.create(ProductsService.class);    }    private RestAdapter.Builder newRestAdapterBuilder(ObjectMapper objectMapper) {        return new RestAdapter.Builder()                       .setLogLevel(RestAdapter.LogLevel.NONE)                       .setConverter(new JacksonConverter(objectMapper));    }}

现在,在您注入ProductsService的任何地方,您都需要使用@named(PRODUCTS)或@named(SUBSIDIARIES)注释依赖项,具体取决于您需要的变体.当然,除了@named注释,您还可以创建自己的自定义注释并使用它们.请参阅“限定符”下的here.

要稍微展平模块,可以将RestAdapter的创建移动到provIDe * Service()方法中,并取消提供* RestAdapter()方法.当然,除非你需要RestAdapters作为模块之外的依赖项.

总结

以上是内存溢出为你收集整理的android – 如何使用Retrofit解决多个API端点?全部内容,希望文章能够帮你解决android – 如何使用Retrofit解决多个API端点?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1135017.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存