Android Dagger 2在改装中更新基本url

Android Dagger 2在改装中更新基本url,android,retrofit,dagger-2,Android,Retrofit,Dagger 2,我正在创建一个新的MVP项目,并使用Dagger 2进行改造,但我面临的问题是,应用程序应该从服务器获取基本URL,并开始调用网络API 这里的问题是,我无法在运行时更新URL! 我想出的最好的解决方案是更新URL,但在下一次运行应用程序时 我尝试了StackOverFlow上存在的许多想法和解决方案,但都没有成功 private final Application mApplication; private String mBaseUrl; public ApplicationModule

我正在创建一个新的MVP项目,并使用Dagger 2进行改造,但我面临的问题是,应用程序应该从服务器获取基本URL,并开始调用网络API

这里的问题是,我无法在运行时更新URL! 我想出的最好的解决方案是更新URL,但在下一次运行应用程序时

我尝试了StackOverFlow上存在的许多想法和解决方案,但都没有成功

private final Application mApplication;
private String mBaseUrl;


public ApplicationModule(Application application, String baseUrl) {
    mApplication = application;
    mBaseUrl = baseUrl;
}


@Provides
@Singleton
OkHttpClient providesOkHttpClient() {
    OkHttpClient.Builder client = new OkHttpClient.Builder();

    client.readTimeout(Constants.TIMEOUT, TimeUnit.SECONDS);
    client.connectTimeout(Constants.TIMEOUT, TimeUnit.SECONDS);

    return client.build();
}


@Provides
@RetrofitGSON
Retrofit providesRetrofit(OkHttpClient okHttpClient) {
    return new Retrofit.Builder()
            .baseUrl(mBaseUrl)
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(new GsonBuilder()
                            .setPrettyPrinting()
                            .create()
                    )
            )
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(okHttpClient)
            .build();
}

任何帮助都将不胜感激

对于所有遇到这种问题的人,下面是解决方法。 您需要覆盖okhttp拦截器

@Singleton
public class ExampleInterceptor implements Interceptor {
    private static ExampleInterceptor sInterceptor;
    private String mScheme;
    private String mHost;

    @Inject
    public static ExampleInterceptor get() {
        if (sInterceptor == null) {
            sInterceptor = new ExampleInterceptor();
         }
      return sInterceptor;
    }

    private ExampleInterceptor() {
         // Intentionally blank
    }

    public void setInterceptor(String url) {
        HttpUrl httpUrl = HttpUrl.parse(url);
        mScheme = httpUrl.scheme();
        mHost = httpUrl.host();
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();

        // If new Base URL is properly formatted than replace with old one
        if (mScheme != null && mHost != null) {
            HttpUrl newUrl = original.url().newBuilder()
                .scheme(mScheme)
                .host(mHost)
                .build();
        original = original.newBuilder()
                .url(newUrl)
                .build();
        }
     return chain.proceed(original);
    }
}

对于所有有这种问题的人,这里是如何解决的。 您需要覆盖okhttp拦截器

@Singleton
public class ExampleInterceptor implements Interceptor {
    private static ExampleInterceptor sInterceptor;
    private String mScheme;
    private String mHost;

    @Inject
    public static ExampleInterceptor get() {
        if (sInterceptor == null) {
            sInterceptor = new ExampleInterceptor();
         }
      return sInterceptor;
    }

    private ExampleInterceptor() {
         // Intentionally blank
    }

    public void setInterceptor(String url) {
        HttpUrl httpUrl = HttpUrl.parse(url);
        mScheme = httpUrl.scheme();
        mHost = httpUrl.host();
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();

        // If new Base URL is properly formatted than replace with old one
        if (mScheme != null && mHost != null) {
            HttpUrl newUrl = original.url().newBuilder()
                .scheme(mScheme)
                .host(mHost)
                .build();
        original = original.newBuilder()
                .url(newUrl)
                .build();
        }
     return chain.proceed(original);
    }
}