【问题标题】:BaseUrl doesn't changed dynamically Dagger 2 & retrofitBaseUrl 不会动态更改 Dagger 2 和改造
【发布时间】:2017-08-21 14:14:23
【问题描述】:

我正在尝试动态更改 android 应用程序的 baseUrl,但 url 没有更改。

我从这里参考了 David 的答案,OkHttp 方法 Dagger + Retrofit dynamic URLSet dynamic base url using Retrofit 2.0 and Dagger 2,但仍然没有运气。

最初,应用程序指向网址“https://imd.com/yeddydemo/wpApp/”,这是我们的应用程序基本网址。

在做了一些谷歌搜索后,我编写了以下代码来将应用程序基本 url 指向https://imd.com/rahudemo/wpApp/,但它不能正常工作:

谁能指出我做错的地方。在此先感谢:)

修改基础url的方法:

  public void changeUrlConfiguration(String name){

 NetComponent netComponent = DaggerNetComponent.builder()
                .apiModule(new ApiModule("https://imd.com/rahudemo/wpApp/"))
                .appModule(new AppModule((ImdPoApp)homeActivity.getApplication()))
                .storageModule(new StorageModule((ImdPoApp)homeActivity.getApplication()))
                .build();

        ApiStories service = netComponent.retrofit().create(ApiStories.class);
        HostSelectionInterceptor interceptor = netComponent.interceptor();

        interceptor.setHost("https://imd.com/rahudemo/wpApp/","8WAtp8nUEOrzSu67t9tGITEo");

}

拦截器类

public final class HostSelectionInterceptor implements Interceptor {
    private volatile String host;
    private String authKey;

    public void setHost(String host,String authKey) {
        this.host = host;
        this.authKey=authKey;
        new ApiModule(host);
    }


    @Override public okhttp3.Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        String host = this.host;
        if (host != null) {
            HttpUrl newUrl = HttpUrl.parse(host);

            request = request.newBuilder()
                    .url(newUrl)
                    .build();
        }
        return chain.proceed(request);
    }

}

ApiModule

@Module
public class ApiModule {
    String mBaseUrl;
    HostSelectionInterceptor sInterceptor;
    public ApiModule(String mBaseUrl) {
        this.mBaseUrl = mBaseUrl;
    }

    @Provides
    @Singleton
    Cache provideHttpCache(Application application) {/*..*/}

    @Provides
    @Singleton
    Gson provideGson()  {/*..*/}

    @Provides
    @Singleton
    OkHttpClient provideOkhttpClient(Cache cache, HostSelectionInterceptor hostSelectionInterceptor) {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.addInterceptor(chain -> {
            Request request = chain.request();
            Request.Builder builder = request.newBuilder().addHeader("Authkey", "8WAtp8nUEOrzSu67t9tGITEo");
            return chain.proceed(builder.build());
        });

        client.addInterceptor(hostSelectionInterceptor);

        client.cache(cache);
        return client.build();
    }

    @Provides
    @Singleton
    HostSelectionInterceptor provideInterceptor() {
        if (sInterceptor == null) {
            sInterceptor = new HostSelectionInterceptor();
        }
        return sInterceptor;
    }


    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
    }

}

【问题讨论】:

  • 您正在方法中创建一个新的NetComponent。当组件范围结束时(即方法结束时),对该组件中的任何 object 所做的所有更改都将丢失

标签: android dependency-injection retrofit2 interceptor dagger-2


【解决方案1】:

在您的方法中传递要更改为 newbaseurl 的基本 url

 public void changeUrlConfiguration(String name, String newbaseurl){
    NetComponent netComponent = DaggerNetComponent.builder()
                .apiModule(new ApiModule(newbaseurl))
                .appModule(new 
    AppModule((ImdPoApp)homeActivity.getApplication()))
                .storageModule(new StorageModule((ImdPoApp)homeActivity.getApplication()))
                .build();

         ApiStories service = netComponent.retrofit().create(A

piStories.class);
        HostSelectionInterceptor interceptor = netComponent.interceptor();

        interceptor.setHost(newbaseurl,"8WAtp8nUEOrzSu67t9tGITEo");

}

【讨论】:

  • 在调用 changeUrlConfiguration 时是的
【解决方案2】:

通过如下更改我的拦截器和 ChangeUrlConfiguration 方法,我可以更改 BaseUrl

拦截器方法

 @Override public okhttp3.Response intercept(Chain chain) throws IOException {

        Request request = chain.request();
        String host = this.host;
        if (host != null) {
            HttpUrl newUrl = request.url().newBuilder()
                    .removePathSegment(0).removePathSegment(0).removePathSegment(0).addPathSegments(host).addPathSegment("wpApp").addEncodedPathSegment("api.php")
                    .build();
            request = request.newBuilder()
                    .url(newUrl).addHeader("Authkey", "8WAtp8nU")
                    .build();
        } else {
            request = request.newBuilder().addHeader("Authkey", "8WAtp8nU")
                    .build();
        }
        try {
            return chain.proceed(request);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }

changeUrlConfiguration

public void changeUrlConfiguration(String constituencyName){

String newbaseurl="https://imdstar.com/rahuldemo/wpApp/";
hostSelectionInterceptor.setHost(newbaseurl,"8WAtp8nUEOrzSu67t9tGITEzIdgr6huIpXqo");

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 2015-12-10
    相关资源
    最近更新 更多