java - Changing baseUrl dynamically using retrofit & dagger2 -


i'm using retrofit & dagger2 application.i want change baseurl of application dynamically based on user selects in spinner.

after spending couple of hours on internet came conclusion possible change baseurl dynamically.

the dependency injection looks this:

apimodule

  @module public class apimodule {   string mbaseurl;      public apimodule(string mbaseurl) {         this.mbaseurl = mbaseurl;     }  @provides     @singleton     okhttpclient provideokhttpclient(cache cache) {         okhttpclient.builder client = new okhttpclient.builder();                    httplogginginterceptor logging = new httplogginginterceptor();         // set desired log level         logging.setlevel(httplogginginterceptor.level.body);         client.addinterceptor(logging);         client.cache(cache);         return client.build();        }      @provides     @singleton     retrofit provideretrofit(okhttpclient okhttpclient) {         return new retrofit.builder()                 .addconverterfactory(gsonconverterfactory.create())                 .baseurl(mbaseurl)                 .client(okhttpclient)                 .build();     } } 

i have created 1 class per reference internet

hostselectioninterceptor.java

import java.io.ioexception; import javax.inject.singleton; import dagger.module; import dagger.provides; import okhttp3.httpurl; import okhttp3.interceptor; import okhttp3.request; /** interceptor allows runtime changes url hostname. */ @module(includes = {apimodule.class}) public final class hostselectioninterceptor implements interceptor {     private volatile string host;      @provides     @singleton     public string sethost(string host) {         this.host = host;         return this.host;     }      public string gethost() {         return host;     }      @provides     @singleton     @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();*/             httpurl newurl = httpurl.parse(host);             request = request.newbuilder()                     .url(newurl)                     .build();         }         try {             return chain.proceed(request);         } catch (ioexception e) {             e.printstacktrace();         }          return null;     } } 

now, question how can use hostselectioninterceptor change baseurl on changing spinner.

request.builder.url - request url. it's added base_url.

to dynamically change base url, have recreate retrofit object.

checkout mock server i've been working on: https://github.com/macieknajbar/mockserver/blob/master/app/src/main/java/com/example/mockserver/rest/server/mockserver.kt

run tests , make changes own use (to replace base url, not responses).


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -