2017-04-21 2 views
1

pour mon projet Je dois utiliser 2 points de terminaison API différents. Maintenant, j'ai implémenté le code dagger2 qui fonctionne avec seulement 1 point d'extrémité. Est-ce que quelqu'un sait, comment puis-je ajouter un autre point de terminaison ici dans mon code correctement?points de terminaison multiples dagger2 retrofit

Voici mon dossier de candidature:

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

}

Voici mon fichier 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); 
} 
} 

Voici mon fichier de service où j'utilise des appels retrofit:

public class FirebaseService { 

public static final String ENDPOINT = ""; 
public static final String ENDPOINT_ADS = ""; 
private static FirebaseService.Firebase firebase; 

public interface Firebase { 

} 
} 

Après mon essayez d'implémenter un autre point de terminaison. Cette erreur apparaît maintenant:

retrofit2.Retrofit cannot be provided without an @Inject constructor or from an @Provides-annotated method.

EDIT

@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

Afficher le code, vous '@' Inject' cas Retrofit'. – azizbekian

+0

En utilisant une seule URL de conversion, j'ai injecté des instances Retrofit dans mon gestionnaire de données. Vous pouvez le voir dans EDIT. Je n'ai pas utilisé l'annotation @Inject et cela a fonctionné sans cela –

Répondre

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

Eh bien, maintenant Dagger est confus, parce qu'il ne peut pas savoir ce que Retrofit pour fournir à votre DataManager.

Vous avez déclaré deux instances @NamedRetrofit dans votre module. Maintenant, vous devez spécifier exactement qui Retrofit instance à être transmis à DataManager:

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

Merci! maintenant cette erreur apparaît: Erreur : my.github.tomas.talisproject.data.remote.FirebaseService.Firebase ne peut pas être fourni sans une méthode @ Provides-annotated. –

+0

Voyez comment vous fournissez une instance 'Firebase' dans votre module. Vous l'avez annoté avec '@Named (FIREBASE)', cela signifie que vous devez également l'annoter dans le paramètre constructeur. – azizbekian

+0

merci azizbekian pour votre aide, j'ai édité tout ce que vous avez dit dans mon message, mais toujours la même erreur apparaît ... –