Android implementing HostSelectionInterceptor for dynamic change url using Dagger 2

穿精又带淫゛_ 提交于 2019-12-11 05:26:05

问题


i just learn about how can i implementing Retrofit with Dagger2 to set dynamic change url on this reference

i try to make simple module with HostSelectionInterceptor class to use that on Dagger2, but i can't make that correctly and i get error:

my NetworkModule:

@Module(includes = ContextModule.class)
public class NetworkModule {
    @Provides
    @AlachiqApplicationScope
    public HttpLoggingInterceptor loggingInterceptor() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Timber.e(message);
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        return interceptor;
    }

    ...

    @Provides
    @AlachiqApplicationScope
    public HostSelectionInterceptor hostSelectionInterceptor() {
        return new HostSelectionInterceptor();
    }

    @Provides
    @AlachiqApplicationScope
    public OkHttpClient okHttpClient(HostSelectionInterceptor hostInterceptor, HttpLoggingInterceptor loggingInterceptor, Cache cache) {
        return new OkHttpClient.Builder()
                .addInterceptor(hostInterceptor)
                .addInterceptor(loggingInterceptor)
                .connectTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .cache(cache)
                .build();
    }
}

and HostSelectionInterceptor module:

@Module(includes = {NetworkModule.class})
public final class HostSelectionInterceptor implements Interceptor {
    private volatile String host;

    @Provides
    @AlachiqApplicationScope
    public String setHost(String host) {
        this.host = host;
        return this.host;
    }

    public String getHost() {
        return host;
    }

    @Provides
    @AlachiqApplicationScope
    @Override
    public okhttp3.Response intercept(Chain chain) {
        Request request = chain.request();
        String  host    = getHost();
        if (host != null) {
            HttpUrl newUrl = request.url().newBuilder()
                    .host(host)
                    .build();
            request = request.newBuilder()
                    .url(newUrl)
                    .build();
        }
        try {
            return chain.proceed(request);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

i get this error now:

java.lang.IllegalArgumentException: unexpected host: http://myUrl.com/ at okhttp3.HttpUrl$Builder.host(HttpUrl.java:754)

problem is set host by setHost method on this line of code:

HttpUrl newUrl = request.url().newBuilder()
        .host(host)
        .build();

回答1:


Based on this github comment, the solution is to replace

        HttpUrl newUrl = request.url().newBuilder()
                .host(host)
                .build();

with

        HttpUrl newUrl = HttpUrl.parse(host);


来源:https://stackoverflow.com/questions/45519852/android-implementing-hostselectioninterceptor-for-dynamic-change-url-using-dagge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!