Set dynamic base url using Retrofit 2.0 and Dagger 2

前端 未结 8 587
南旧
南旧 2020-11-27 12:36

I\'m trying to perform a login action using Retrofit 2.0 using Dagger 2

Here\'s how I set up Retrofit dependency

@Provides
@Singleton
Retrofit provid         


        
8条回答
  •  佛祖请我去吃肉
    2020-11-27 13:05

    Thanks to @EpicPandaForce for help. If someone is facing IllegalArgumentException, this is my working code.

    public class HostSelectionInterceptor implements Interceptor {
        private volatile String host;
    
        public void setHost(String host) {
            this.host = HttpUrl.parse(host).host();
        }
    
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            String reqUrl = request.url().host();
    
            String host = this.host;
            if (host != null) {
                HttpUrl newUrl = request.url().newBuilder()
                    .host(host)
                    .build();
                request = request.newBuilder()
                    .url(newUrl)
                    .build();
            }
            return chain.proceed(request);
        }
    }
    

提交回复
热议问题