-1

I'm trying to change the baseUrl of an android app dynamically but the url doesn't changed.

I'm taking the reference of David's answer from here,the OkHttp Approach Dagger + Retrofit dynamic URL and Set dynamic base url using Retrofit 2.0 and Dagger 2, but still no luck.

Initially the app point to the urls "https://imd.com/yeddydemo/wpApp/",which is our app base url.

After doing some googling i have written the following code to change the app base url points to https://imd.com/rahudemo/wpApp/ but it doesn't works correctly:

Can anyone please point out where i'm doing wrong.Thanks in Advance:)

Method to change the base url:

  public void changeUrlConfiguration(String name){

 NetComponent netComponent = DaggerNetComponent.builder()
                .apiModule(new ApiModule("https://imd.com/rahudemo/wpApp/"))
                .appModule(new AppModule((ImdPoApp)homeActivity.getApplication()))
                .storageModule(new StorageModule((ImdPoApp)homeActivity.getApplication()))
                .build();

        ApiStories service = netComponent.retrofit().create(ApiStories.class);
        HostSelectionInterceptor interceptor = netComponent.interceptor();

        interceptor.setHost("https://imd.com/rahudemo/wpApp/","8WAtp8nUEOrzSu67t9tGITEo");

}

Interceptor class

public final class HostSelectionInterceptor implements Interceptor {
    private volatile String host;
    private String authKey;

    public void setHost(String host,String authKey) {
        this.host = host;
        this.authKey=authKey;
        new ApiModule(host);
    }


    @Override public okhttp3.Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        String host = this.host;
        if (host != null) {
            HttpUrl newUrl = HttpUrl.parse(host);

            request = request.newBuilder()
                    .url(newUrl)
                    .build();
        }
        return chain.proceed(request);
    }

}

ApiModule

@Module
public class ApiModule {
    String mBaseUrl;
    HostSelectionInterceptor sInterceptor;
    public ApiModule(String mBaseUrl) {
        this.mBaseUrl = mBaseUrl;
    }

    @Provides
    @Singleton
    Cache provideHttpCache(Application application) {/*..*/}

    @Provides
    @Singleton
    Gson provideGson()  {/*..*/}

    @Provides
    @Singleton
    OkHttpClient provideOkhttpClient(Cache cache, HostSelectionInterceptor hostSelectionInterceptor) {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.addInterceptor(chain -> {
            Request request = chain.request();
            Request.Builder builder = request.newBuilder().addHeader("Authkey", "8WAtp8nUEOrzSu67t9tGITEo");
            return chain.proceed(builder.build());
        });

        client.addInterceptor(hostSelectionInterceptor);

        client.cache(cache);
        return client.build();
    }

    @Provides
    @Singleton
    HostSelectionInterceptor provideInterceptor() {
        if (sInterceptor == null) {
            sInterceptor = new HostSelectionInterceptor();
        }
        return sInterceptor;
    }


    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
    }

}
Pranesh Sahu
  • 595
  • 5
  • 26
  • You're creating a new `NetComponent` inside your method. All changes made to any `object` living in that component will be lost when the component scope is over (this is, when the method ends) – Alberto S. Aug 22 '17 at 06:22

2 Answers2

0

In your method pass the base url want to change to newbaseurl

 public void changeUrlConfiguration(String name, String newbaseurl){
    NetComponent netComponent = DaggerNetComponent.builder()
                .apiModule(new ApiModule(newbaseurl))
                .appModule(new 
    AppModule((ImdPoApp)homeActivity.getApplication()))
                .storageModule(new StorageModule((ImdPoApp)homeActivity.getApplication()))
                .build();

         ApiStories service = netComponent.retrofit().create(A

piStories.class);
        HostSelectionInterceptor interceptor = netComponent.interceptor();

        interceptor.setHost(newbaseurl,"8WAtp8nUEOrzSu67t9tGITEo");

}
0

By Changing my interceptor & ChangeUrlConfiguration method as below i can able to change the BaseUrl

Interceptor method

 @Override public okhttp3.Response intercept(Chain chain) throws IOException {

        Request request = chain.request();
        String host = this.host;
        if (host != null) {
            HttpUrl newUrl = request.url().newBuilder()
                    .removePathSegment(0).removePathSegment(0).removePathSegment(0).addPathSegments(host).addPathSegment("wpApp").addEncodedPathSegment("api.php")
                    .build();
            request = request.newBuilder()
                    .url(newUrl).addHeader("Authkey", "8WAtp8nU")
                    .build();
        } else {
            request = request.newBuilder().addHeader("Authkey", "8WAtp8nU")
                    .build();
        }
        try {
            return chain.proceed(request);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }

changeUrlConfiguration

public void changeUrlConfiguration(String constituencyName){

String newbaseurl="https://imdstar.com/rahuldemo/wpApp/";
hostSelectionInterceptor.setHost(newbaseurl,"8WAtp8nUEOrzSu67t9tGITEzIdgr6huIpXqo");

}

Pranesh Sahu
  • 595
  • 5
  • 26