Android使用Dagger 2为动态更改网址实现HostSelectionInterceptor [英] Android implementing HostSelectionInterceptor for dynamic change url using Dagger 2

查看:127
本文介绍了Android使用Dagger 2为动态更改网址实现HostSelectionInterceptor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是了解如何使用Dagger2实现 Retrofit 以在此引用

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

我尝试使用 HostSelectionInterceptor 类制作简单的模块以在其上使用Dagger2,但我无法正确实现,并且出现错误:

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:

我的 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)

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

问题已设置在此代码行上通过 setHost 方法托管:

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

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


推荐答案

基于此github评论,解决方案是替换

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

        HttpUrl newUrl = HttpUrl.parse(host);

这篇关于Android使用Dagger 2为动态更改网址实现HostSelectionInterceptor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆