【问题标题】:Android implementing HostSelectionInterceptor for dynamic change url using Dagger 2Android 使用 Dagger 2 为动态更改 url 实现 HostSelectionInterceptor
【发布时间】:2017-08-05 08:25:13
【问题描述】:

我刚刚了解如何使用 Dagger2 实现 Retrofit 以在此 reference 上设置动态更改 url

我尝试使用 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();
    }
}

HostSelectionInterceptor 模块:

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

我现在收到此错误:

java.lang.IllegalArgumentException:意外主机:http://myUrl.com/ okhttp3.HttpUrl$Builder.host(HttpUrl.java:754)

问题是在这行代码通过setHost方法设置主机:

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

【问题讨论】:

    标签: android retrofit retrofit2 dagger-2 dagger


    【解决方案1】:

    基于this github comment,解决办法是替换

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

            HttpUrl newUrl = HttpUrl.parse(host);
    

    【讨论】:

    • 谢谢,这个问题解决了,但是我在获取请求时遇到了另一个问题,在通过intercepter改造更改主机后尝试通过我设置为改造的旧网址获取请求,请看这个屏幕截图rupload.ir/upload/lknupff8l09lygr4gnso.png,代码:if (host != null) { HttpUrl newUrl = HttpUrl.parse(host); request = request.newBuilder() .url(newUrl) .build(); }
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-02
      • 2016-07-29
      • 2016-08-14
      • 2017-06-23
      • 1970-01-01
      • 2012-01-02
      • 2020-05-22
      • 2015-12-10
      相关资源
      最近更新 更多