我使用MPV
+ Dagger2
+ Retrofit2
我创建了改造serviseApi
public interface MailService {
@POST("sendEmail")
Observable<Boolean> sendMail(@Body MailRequest mail);
}
之后我创建Retrofit2
instanse(我有选项 - 在设置中更改baseUrl,timeOut值等,所以我不能像Singleton一样创建Retrofit)
public class RestApiFactoryImpl implements RestApiFactory{
private Retrofit retrofit;
private Utils utils;
public RestApiFactoryImpl(Utils utils) {
this.utils = utils;
this.retrofit = createRetrofit(providesOkHttpClient(loggingInterceptor()));
}
private HttpLoggingInterceptor loggingInterceptor() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> Log.e("loggingInterceptor", message));
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return interceptor;
}
private OkHttpClient providesOkHttpClient(HttpLoggingInterceptor httpLoggingInterceptor) {
User user = utils.getSettings();
return new OkHttpClient.Builder()
.connectTimeout(Long.valueOf(user.getTimeOut()), TimeUnit.SECONDS)
.writeTimeout(Long.valueOf(user.getTimeOut()), TimeUnit.SECONDS)
.readTimeout(Long.valueOf(user.getTimeOut()), TimeUnit.SECONDS)
.addInterceptor(httpLoggingInterceptor)
.build();
}
private Retrofit createRetrofit(OkHttpClient okHttpClient) {
User user = utils.getSettings();
String host = user.getHost();
if (!host.endsWith("/")) host += "/";
return new Retrofit.Builder()
.baseUrl(host)
.addConverterFactory(JacksonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient).build();
}
private Retrofit getRetrofit() {
if (retrofit == null) {
retrofit = createRetrofit(providesOkHttpClient(loggingInterceptor()));
}
return retrofit;
}
public void clearRetrofit() {
retrofit = null;
}
@Override
public MailService getMailService() {
return getRetrofit().create(MailService.class);
}
}
Wheb我更新设置 - 我使用新设置重新创建Retrofit instanse。我创建模块:
@Module
class NetworkModule {
@Singleton
@Provides
RestApiFactory providesRestApiFactory(Utils utils) {
return new RestApiFactoryImpl(utils);
}
}
现在我想在我的存储库中使用它。我创建了新的repository
并传递了RestApiFactory
@Singleton
@Provides
MyRepository provideMyRepository(RestApiFactory restApiFactory) {
return new MyRepositoryImpl(restApiFactory);
}
并在Retrofit
内使用apiServices
MyRepositoryImpl
,如下所示:
public class MyRepositoryImpl implements MyRepository {
private RestApiFactory restApiFactory;
@Inject
public MyRepositoryImpl(RestApiFactory restApiFactory) {
this.restApiFactory = restApiFactory;
}
@Override
public Observable<Boolean> sendEmail(MailRequest mailRequest) {
return restApiFactory.getMailService().sendMail(mailRequest);
}
}
但我想要另一种方式。我不想RestApiFactory
和MailService
例如:
@Provides
MailService providesRestApiFactory(RestApiFactory restApiFactory) {
return restApiFactory.getMailService();
}
@Singleton
@Provides
MyRepository provideMyRepository(MailService mailService) {
return new MyRepositoryImpl(mailService);
}
public class MyRepositoryImpl implements MyRepository {
private MailService ailService;
@Inject
public MyRepositoryImpl(MailService mailService) {
this.mailService= mailService;
}
@Override
public Observable<Boolean> sendEmail(MailRequest mailRequest) {
return mailService.sendMail(mailRequest);
}
}
但是MyRepository
是单身并且只调用providesRestApiFactory
方法一次。如果我更改设置,则不会更新。