代码之家  ›  专栏  ›  技术社区  ›  DolDurma

Android使用Dagger 2实现动态更改url的HostSelectionInterceptor

  •  1
  • DolDurma  · 技术社区  · 8 年前

    我只是了解如何实施 Retrofit 使用Dagger2在该服务器上设置动态更改url reference

    HostSelectionInterceptor 类在Dagger2上使用该选项,但我无法正确执行该选项,因此出现错误:

    我的 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();
        }
    }
    

    主机选择接收器 模块:

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

    http://myUrl.com/ 在okhttp3.HttpUrl$Builder.host(HttpUrl.java:754)

    问题由主机设置 setHost 此代码行上的方法:

    HttpUrl newUrl = request.url().newBuilder()
            .host(host)
            .build();
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   EpicPandaForce Jigar Joshi    8 年前

    基于 this github comment

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

            HttpUrl newUrl = HttpUrl.parse(host);
    
        2
  •  0
  •   Veton Neziri    5 年前

    您应该像这样使用拦截器:

    class HostSelectionInterceptor: Interceptor {
        override fun intercept(chain: Interceptor.Chain): Response {
            apiHost?.let { host ->
                val request = chain.request()
                val newUrl = request.url.newBuilder().host(host).build()
                val newRequest = request.newBuilder().url(newUrl).build()
                return chain.proceed(newRequest)
            }
            throw IOException("Unknown Server")
        }
    }
    
    You just need to change at runtime the apiHost variable (var apiHost = "example.com"). Then add this interceptor to OkHttpClient builder:
    
    val okHttpClient = OkHttpClient.Builder()
        .addInterceptor(HostSelectionInterceptor())
        .build()