如何将 Retrofit 对象直接注入存储库 class?
How to inject a Retrofit object directly into a repository class?
我想将一个 Retrofit
对象直接注入我的 MyRepository
class,但我总是得到一个 NullPointerException
。这是我试过的。
这是我的 AppModule
class:
@Module
public class AppModule {
@Singleton
@Provides
static Retrofit provideRetrofitInstance(){
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
}
这是我的视图模型class:
public class MyViewModel extends AndroidViewModel {
LiveData<Data> myLiveData;
MyViewModel(Application application, City city) {
super(application);
myLiveData = myRepository.addDataToLiveData(city);
}
LiveData<Data> getLiveData() {
return myLiveData;
}
}
这是我的存储库 class,我想在其中注入 Retofit:
public class MyRepository {
private String myTex;
@Inject
private Retrofit retrofit;
public MyRepository(String myText) {
this.myText = myText;
}
LiveData<Data> addDataToLiveData(City city) {
//Make api call using retrofit
}
}
编辑:
这就是我在 activity class 中实例化 ViewModel
的方式:
MyRepository repository = new MyRepository("MyText");
Application application = activity.getApplication();
MyViewModelFactory factory = new MyViewModelFactory(application, repository);
MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
使您的存储库可注入是最简单的解决方案,它还允许您在使用它的地方注入它,在您的 ViewModel
s 或 Interactor
s:
@Singleton
public class MyRepository {
private Retrofit retrofit;
@Inject
public MyRepository(Retrofit retrofit) {
this.retrofit = retrofit;
}
LiveData<Data> addDataToLiveData(City city) {
//Make api call using retrofit
}
}
编辑:您可以通过 Dagger 提供文本并将其注入构造函数,就像这样
@Inject
public MyRepository(String myText, Retrofit retrofit)
请注意,您需要使用 @Named
或 @Qualifier
作为字符串。
或者,您可以注入存储库调用 inject(this)
,语法取决于您如何设置 Dagger
somehowGetDaggerComponent().inject(this)
强烈建议您使用第一种解决方案。
我想将一个 Retrofit
对象直接注入我的 MyRepository
class,但我总是得到一个 NullPointerException
。这是我试过的。
这是我的 AppModule
class:
@Module
public class AppModule {
@Singleton
@Provides
static Retrofit provideRetrofitInstance(){
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
}
这是我的视图模型class:
public class MyViewModel extends AndroidViewModel {
LiveData<Data> myLiveData;
MyViewModel(Application application, City city) {
super(application);
myLiveData = myRepository.addDataToLiveData(city);
}
LiveData<Data> getLiveData() {
return myLiveData;
}
}
这是我的存储库 class,我想在其中注入 Retofit:
public class MyRepository {
private String myTex;
@Inject
private Retrofit retrofit;
public MyRepository(String myText) {
this.myText = myText;
}
LiveData<Data> addDataToLiveData(City city) {
//Make api call using retrofit
}
}
编辑:
这就是我在 activity class 中实例化 ViewModel
的方式:
MyRepository repository = new MyRepository("MyText");
Application application = activity.getApplication();
MyViewModelFactory factory = new MyViewModelFactory(application, repository);
MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
使您的存储库可注入是最简单的解决方案,它还允许您在使用它的地方注入它,在您的 ViewModel
s 或 Interactor
s:
@Singleton
public class MyRepository {
private Retrofit retrofit;
@Inject
public MyRepository(Retrofit retrofit) {
this.retrofit = retrofit;
}
LiveData<Data> addDataToLiveData(City city) {
//Make api call using retrofit
}
}
编辑:您可以通过 Dagger 提供文本并将其注入构造函数,就像这样
@Inject
public MyRepository(String myText, Retrofit retrofit)
请注意,您需要使用 @Named
或 @Qualifier
作为字符串。
或者,您可以注入存储库调用 inject(this)
,语法取决于您如何设置 Dagger
somehowGetDaggerComponent().inject(this)
强烈建议您使用第一种解决方案。