对于我的项目,我需要使用 2 个不同的 API 端点。现在我已经实现了仅适用于 1 个端点的 dagger2 代码。有谁知道,如何在我的代码中正确添加另一个端点?
这是我的申请文件:
public class TalisProjectApplication extends Application {
@Inject
DataManager dataManager;
ApplicationComponent applicationComponent;
@Override
public void onCreate() {
super.onCreate();
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this, FirebaseService.ENDPOINT, FirebaseService.ENDPOINT_ADS))
.build();
applicationComponent.inject(this);
}
public static TalisProjectApplication get(Context context) {
return (TalisProjectApplication) context.getApplicationContext();
}
public ApplicationComponent getComponent() {
return applicationComponent;
}
这是我的 dagger2 文件
@Module
public class ApplicationModule {
protected final Application application;
String baseUrl;
String baseAdsUrl;
private LoginActivity activity;
public static final String FIREBASE = "firebase";
public static final String ADS = "ads";
public ApplicationModule(Application app, @Named(FIREBASE) String url, @Named(ADS)String baseAdsUrl) {
application = app;
this.baseUrl = url;
this.baseAdsUrl = baseAdsUrl;
}
@Provides
Application provideApplication() {
return application;
}
@Provides
@ApplicationContext
Context provideContext() {
return application;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
@Provides
@Singleton
Cache provideHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024;
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(logging);
client.cache(cache);
return client.build();
}
@Provides
@Singleton
@Named(FIREBASE)
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(baseUrl)
.client(okHttpClient)
.build();
}
@Provides
@Singleton
@Named(ADS)
Retrofit provideAdsRetrofit(Gson gson, OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(baseAdsUrl)
.client(okHttpClient)
.build();
}
@Provides
@Singleton
@Named(FIREBASE)
FirebaseService.Firebase providesTheFirebaseService(@Named(FIREBASE) Retrofit retrofit) {
return retrofit.create(FirebaseService.Firebase.class);
}
@Provides
@Singleton
@Named(ADS)
FirebaseService.Firebase providesTheAdsService(@Named(ADS) Retrofit retrofit) {
return retrofit.create(FirebaseService.Firebase.class);
}
}
这是我使用改造调用的服务文件:
public class FirebaseService {
public static final String ENDPOINT = "";
public static final String ENDPOINT_ADS = "";
private static FirebaseService.Firebase firebase;
public interface Firebase {
}
}
在我尝试实现另一个端点之后。现在出现此错误:
retrofit2.Retrofit cannot be provided without an @Inject constructor or from an @Provides-annotated method.
编辑
@Singleton
public class DataManager {
private Retrofit firebaseRetrofit;
private Retrofit adsRetrofit;
private FirebaseService.Firebase firebase;
private DatabaseReference databaseRef;
private Application application;
private PreferencesHelper preferencesHelper;
String catId;
@Inject
public DataManager(@Named(FIREBASE) Retrofit firebaseRetrofit, @Named(ADS) Retrofit adsRetrofit, FirebaseService.Firebase firebase, Application application, PreferencesHelper preferencesHelper) {
this.firebaseRetrofit = firebaseRetrofit;
this.adsRetrofit = adsRetrofit;
this.firebase = firebase;
this.application = application;
this.preferencesHelper = preferencesHelper;
}
}
最佳答案
@Inject
public DataManager(Retrofit retrofit, ...) {
...
}
好吧,现在 Dagger 很困惑,因为他不知道要为您的 DataManager
提供什么 Retrofit
。
您在模块中声明了两个 @Named
Retrofit
实例。现在您必须指定要传递给 DataManager
的确切 Retrofit
实例:
@Inject
public DataManager(@Named(FIREBASE) Retrofit firebaseRetrofit,
@Named(ADS) Retrofit adsRetrofit,
... ) {
...
}
关于java - 多端点 dagger2 改造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43538999/