Android implementing HostSelectionInterceptor for

2019-07-27 10:51发布

问题:

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