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

前端之家收集整理的这篇文章主要介绍了android – 如何使用Retrofit解决多个API端点?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的 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作为模块之外的依赖项.

原文链接:https://www.f2er.com/android/314465.html

猜你在找的Android相关文章