【问题标题】:multiple endpoints dagger2 retrofit多端点 dagger2 改造
【发布时间】:2017-04-21 09:30:21
【问题描述】:

对于我的项目,我需要使用 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 不能在没有 @Inject 构造函数的情况下提供 或来自 @Provides 注释的方法。

编辑

@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 Retrofit 实例。
  • 只使用一个改造 url,我在我的数据管理器中注入改造实例。您可以在 EDIT 中看到它。我没有使用@Inject 注释,没有它也可以工作

标签: java api retrofit2 dagger-2 endpoint


【解决方案1】:
@Inject
public DataManager(Retrofit retrofit, ...) {
    ...
}

好吧,现在 Dagger 很困惑,因为他不知道 Retrofit 可以向您的 DataManager 提供什么。

您在模块中声明了两个 @Named Retrofit 实例。现在您必须指定要传递给DataManager 的确切Retrofit 实例:

@Inject
public DataManager(@Named(FIREBASE) Retrofit firebaseRetrofit,
                   @Named(ADS) Retrofit adsRetrofit,
                   ... ) {
    ...
}

【讨论】:

  • 谢谢!现在出现这个错误:error: my.github.tomas.talisproject.data.remote.FirebaseService.Firebase cannot be provided without an @Provides-annotated method.
  • 查看您如何在模块中提供Firebase 实例。你已经用@Named(FIREBASE)注释了它,这意味着你还必须在构造函数参数中注释它。
  • azizbekian 感谢您的帮助,我编辑了您在帖子中所说的所有内容,但仍然出现相同的错误...
  • @LaurisMacke,I edited everything you said in my post 你还没有。更改为@Named(FIREBASE) FirebaseService.Firebase firebase
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
  • 2014-11-24
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
相关资源
最近更新 更多