gpt4 book ai didi

android - 如何使用 Retrofit 处理多个 API 端点?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:54 25 4
gpt4 key购买 nike

在我的 Android 项目中,我使用以下 Retrofit ApiModule 用于一个 API 端点。请注意,我使用 Dagger用于注入(inject)依赖项。

@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")。是否可以在重用 ObjectMapperRestAdapter 的同时扩展给定的 ApiModule?还是我不应该延长它?我已经尝试复制模块。但这涉及到我必须复制 EndpointObjectMapper 和...RestAdapter 有一个private 构造函数- 所以我不能。

最佳答案

我猜你可以使用 Named 注释:

@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));
}
}

现在,在注入(inject) ProductsService 的任何地方,您都需要使用 @Named(PRODUCTS)@Named(SUBSIDIARIES) 注释依赖关系,取决于您需要哪种变体。当然,除了 @Named 注释之外,您还可以创建自己的自定义注释并使用它们。参见 here在“限定符”下。

要稍微扁平化您的模块,您可以将 RestAdapter 的创建移动到 provide*Service() 方法中,并摆脱 provide*RestAdapter() 方法.当然,除非您需要 RestAdapters 作为模块外部的依赖项。

关于android - 如何使用 Retrofit 处理多个 API 端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27399633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com