对于我的项目,我需要使用两个不同的API端点。现在,我已经实现了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 {
}
}在我尝试实现另一个端点之后。现在出现此错误:
如果没有@Inject构造函数或@Provides注释的方法,就不能提供annotated。
编辑
@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;
}
}发布于 2017-04-21 10:10:31
@Inject
public DataManager(Retrofit retrofit, ...) {
...
}好吧,现在达格很困惑,因为他不知道要向你的Retrofit提供什么DataManager。
您已经在模块中声明了两个@Named Retrofit实例。现在您必须指定要传递给Retrofit的确切的DataManager实例。
@Inject
public DataManager(@Named(FIREBASE) Retrofit firebaseRetrofit,
@Named(ADS) Retrofit adsRetrofit,
... ) {
...
}https://stackoverflow.com/questions/43538999
复制相似问题