Search code examples
androidkotlinretrofit2koin

How i change the retrofit URL at runtime


I need change the URL base in retrofit, i'm using koin to create a retrofit module on app startup and i want change this url in runtime.

I already tried change the baseUrl("http://192.168.192.168/") to baseUrl("http://")and change the url on retrofit call but my app crashs and return illegal URL error.

This is my fun to create the builder

fun createRetrofit(okHttpClient: OkHttpClient): Retrofit {
    return  Retrofit.Builder()
    .baseUrl("http://192.168.192.168/")//i need change this at runtime
    .client(okHttpClient)
    .addConverterFactory(GsonConverterFactory.create())            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}

create a bean to my module

val retrofitModule: Module = applicationContext {
    bean { createRetrofit(get()) }
}

and start the koin:

startKoin(application = this,
  modules = listOf(retrofitModule, ...)
)

someone can i help me with this?


Solution

  • you must have to add these lines in your code:

    First Step: Add the new CallAdapter RxJavaCallAdapterFactory.create() when building a Retrofit instance.

    public static final String BASE_URL = "http://google.com/";
    public static Retrofit getClient(String baseUrl) {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
    

    Next step: Update the APIService for example:-> savePost(String title, String body, String userId) method to become an Observable.

    public interface APIService {
        @GET
        Call<ResponseBody> list(@Url String url);
    //or
    
    @POST("/posts")
    @FormUrlEncoded
    Observable<Post> savePost(@Field("title") String title,
                              @Field("body") String body,
                              @Field("userId") long userId);
    }
    

    Final step: When making the requests, our anonymous subscriber responds to the observable's stream which emits event.

    public void sendPost(String title, String body) {
    
        // RxJava
        mAPIService.savePost(title, body, 1).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Post>() {
                    @Override
                    public void onCompleted() {
    
                    }
    
                    @Override
                    public void onError(Throwable e) {
    
                    }
    
                    @Override
                    public void onNext(Post post) {
                        showResponse(post.toString());
                    }
                });
    }
    

    this is way you build your dynamic urls: want to learn more details full description link: Sending Data With Retrofit 2 HTTP Client for Android

    and See base URL for details of how the value will be resolved against a base URL to create the full endpoint URL.

    if you are doing using kotlin: follow this link. dynamic urls at Runtime with Retrofit 2