多端点dagger2改造

时间:2017-04-21 09:30:21

标签: java api retrofit2 dagger-2 endpoint

对于我的项目,我需要使用2个不同的API端点。现在我已经实现了dagger2代码,它只能使用1个端点。有没有人知道,如何在我的代码中正确添加另一个端点?

这是我的申请文件:

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构造函数,则无法提供retrofit2.Retrofit   或者来自@ Provide-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;
}
}

1 个答案:

答案 0 :(得分:1)

if($_POST['type'] == 'login'){
  $login_row = @mysql_fetch_assoc(@mysql_query("SELECT * FROM `person` WHERE `user` = '".$_POST['user']."' AND `password` = '".$_POST['Passwd']."'"));
  if($login_row)
  {
   $_SESSION['user'] = $login_row['user_name'];
   echo 'welcome';
  }else
  {
   echo 'Incorrect username/password. please login again';
  }
 }

嗯,现在Dagger很困惑,因为他无法知道@Inject public DataManager(Retrofit retrofit, ...) { ... } 要提供什么Retrofit

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

DataManager