2016-12-20 3 views
1

Ich bin mit der Fertigstellung Dagger 2 Retrofit2 Implementierung mit mvp stecken, wo meine App Aufrufe an zwei verschiedene Apis, in jeweiligen Fragmente. I haben Qualifier Annotationen definiert eine Instanz von Retrofit für jede API-Aufruf zu binden, wie untenDolch 2 mehrere Retrofit-Instanzen mit verschiedenen URLs für das Schlagen verschiedener APIs mit Konstruktor Injektion

@Qualifier 
@Retention(RUNTIME) 
public @interface FirstApi{} 

@Qualifier 
@Retention(RUNTIME) 
public @interface SecondApi{} 

jede Retrofit-Bindung in der ApplicationModule

public class ApplicationModule { 

private String mBaseUrlFirstApi; 
private String mBaseUrlSecondApi; 
private Context mContext; 

public ApplicationModule(Context context) { 
     mContext = context; 
    } 

... 
@Provides 
@FirstApi Retrofit provideFirstApiRetrofit(…) {…} 
@Provides 
@SecondApi Retrofit provideSecondApiRetrofit(…) {…} 

.... 
} 

ApplicationComponent

@Singleton 
@Component(modules = ApplicationModule.class) 
public interface ApplicationComponent { 

    Retrofit exposeRetrofit(); 

    Context exposeContext(); 
} 

Scope

@Scope 
@Retention(RetentionPolicy.RUNTIME) 
public @interface PerActivity { 
} 

Providing ApiService

@Module 
public class MyModule { 

    @PerActivity 
    @Provides 
    MyAPIService myApiService(Retrofit retrofit) { 
     return retrofit.create(MyAPIService.class); 
    } 
} 

Dolch Injector Klasse

@PerActivity 
@Component(modules = MyModule.class, dependencies = ApplicationComponent.class) 
public interface MyComponent { 

    void inject(Fragment1 fragment1); 
    void inject(Fragment2 fragment2); 
    // void inject(Fragment3 fragment3); 
} 

In der Anwendungsklasse, für einen einzelnen Retrofit-API-Aufruf, weiß, dass ich tun:

public class MyApplication extends Application { 

    private ApplicationComponent mApplicationComponent; 

{...} 
private void initializeApplicationComponents() { 
     mApplicationComponent = DaggerApplicationComponent 
       .builder() 
       .applicationModule(new ApplicationModule(this, "http://api1")) 
       .build(); 
    } 

{...} 

public ApplicationComponent getApplicationComponent() { 
     return mApplicationComponent; 
    } 

}

Aber für mehrere URL-API-Aufrufe funktioniert dies nicht

public class MyApplication extends Application { 

    private ApplicationComponent mApplicationComponent; 

{...} 
private void initializeApplicationComponents() { 
     mApplicationComponent = DaggerApplicationComponent 
       .builder() 
       .applicationModule(new ApplicationModule(this, "http://api1")) 
       .applicationModule(new ApplicationModule(this, "http://api2")) 
       .build(); 
    } 

{...} 

public ApplicationComponent getApplicationComponent() { 
     return mApplicationComponent; 
    } 
} 

Weder tut dies

public class MyApplication extends Application { 

    private ApplicationComponent mApplicationComponent; 

{...} 
private void initializeApplicationComponents() { 
     mApplicationComponent = DaggerApplicationComponentApi1 
       .builder() 
       .applicationModule(new ApplicationModule(this, "http://api1")) 
       .build(); 

     mApplicationComponent = DaggerApplicationComponentApi2 
       .builder() 
       .applicationModule(new ApplicationModule(this, "http://api2")) 
       .build(); 
    } 

{...} 

public ApplicationComponent getApplicationComponent() { 
     return mApplicationComponent; 
    } 

}

Auflösen in jedem Fragment Dolch Abhängigkeit wie

so getan wird
protected resolveDaggerDependency() { 
    DaggerFragmentComponent 
      .builder() 
      .applicationComponent(getApplicationComponent) 
      .myModule(new MyModule.class) 
      .build.inject(this); 
} 

BaseFragment Klasse

public abstract class BaseFragment extends Fragment { 

{...} 
{...} 
{...} 

@CallSuper 
protected void onViewReady(Bundle savedInstanceState, Intent intent) { 
    resolveDaggerDependency(); // to be used by child fragments 

protected ApplicationComponent getApplicationComponent() { 
    return ((MyApplication)getActivity().getApplication()).getApplicationComponent; 
} 

} 

Antwort

-1

Bevor Sie

mApplicationComponent = DaggerApplicationComponent 
       .builder() 
       .applicationModule(new ApplicationModule(this, "http://api1")) 
       .applicationModule(new ApplicationModule(this, "http://api2")) 
       .build(); 

das Projekt Anrufen und Code-Vervollständigung verwenden, um zu sehen, was Android Studio sagt Sie für die Komponente Builder zur Verfügung steht. Meine Annahme ist, dass der Konstruktor des Anwendungsmoduls nicht sowohl einen Kontext als auch eine Zeichenfolge akzeptiert. Sobald Sie den Konstruktor ändern, nehmen sagen

ApplicationModule(Context context, String url1, String url2) {...} 

dann alles, was Sie tun müssen, würde Ihre Injektion Code zu ändern, um die zweite Zeile von .applicationModule zu löschen und die erste Zeile ändern, um die neuen Konstruktor übereinstimmen, dh :

mApplicationComponent = DaggerApplicationComponent 
       .builder() 
       .applicationModule(new ApplicationModule(this, "http://api1", "http://api2")) 
       .build(); 
Verwandte Themen