2017-04-21 58 views
1

我的項目我需要使用2個不同的API端點。現在我已經實現了只與1個端點一起工作的dagger2代碼。是否有人知道,我如何在代碼中正確添加另一個端點?多個端點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; 
} 
} 
+0

顯示代碼,在那裏你'@'Inject'實例Retrofit'。 – azizbekian

+0

只使用一個翻新網址,我在我的數據管理器中注入了Retrofit實例。你可以在編輯中看到它。我沒有使用@Inject註釋,它沒有它的工作 –

回答

1
@Inject 
public DataManager(Retrofit retrofit, ...) { 
    ... 
} 

好了,現在匕首是迷茫的,因爲他不知道什麼Retrofit提供給您的DataManager

您已在模塊中聲明瞭兩個@NamedRetrofitRetrofit實例。現在,您必須指定要傳遞給DataManager這正是Retrofit例如:

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

謝謝!現在會出現此錯誤: 錯誤:沒有@提供註釋的方法不能提供my.github.tomas.talisproject.data.remote.FirebaseService.Firebase。 –

+0

瞭解如何在模塊中提供Firebase實例。你用'@Named(FIREBASE)'註解它,這意味着你必須在構造函數參數中註釋它。 – azizbekian

+0

azizbekian謝謝你的幫助,我編輯了你在我的帖子中說的所有內容,但仍然出現同樣的錯誤... –