1

我用MPV+ Dagger2+Retrofit2

我创建改造serviseApi

public interface MailService {
    @POST("sendEmail")
    Observable<Boolean> sendMail(@Body MailRequest mail);
}

之后我创建Retrofit2实例(我有选项 - 在设置中更改 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);
    }
}

当我更新设置时 - 我使用新设置重新创建 Retrofit 实例。我创建模块:

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

但我想要另一种方式。我不想RestApiFactoryMailService例如:

    @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方法一次。如果我更改设置,它不会更新。

4

0 回答 0