How i change the retrofit URL at runtimeRetrofit 2 - Dynamic URLHow to save an Android Activity state using save instance state?How do I center text horizontally and vertically in a TextView?Why is the Android emulator so slow? How can we speed up the Android emulator?How can I open a URL in Android's web browser from my application?How do I fix android.os.NetworkOnMainThreadException?Moshi's Custom Adapter with RxAndroid & Retrofit & KotlinChanging request inside Retrofit CallAdapterRetrofit 2 login post request returns 500 but works well on postmanHow to Change API Base Url at Runtime(Retrofit,Android,Java)?Retrofit 2 - Getting response 200, but list is empty

Are all French verb conjugation tenses and moods practical and efficient?

Should students have access to past exams or an exam bank?

Using Python in a Bash Script

Why did some Apollo missions carry a grenade launcher?

PCB design using code instead of clicking a mouse?

Is it possible to tell if a child will turn into a Hag?

What force enables us to walk? Friction or normal reaction?

What Marvel character has this 'W' symbol?

Word for giving preference to the oldest child

Scam? Checks via Email

What are the closest international airports in different countries?

What is a good example for artistic ND filter applications?

Coworker mumbles to herself when working, how to ask her to stop?

Why don't short runways use ramps for takeoff?

Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

Would it take any sort of amendment to make DC a state?

Why would an invisible personal shield be necessary?

Applications of pure mathematics in operations research

Correct word for a little toy that always stands up?

Can I attune a Circlet of Human Perfection to my animated skeletons to allow them to blend in and speak?

Is it possible for a particle to decay via gravity?

Can a US President, after impeachment and removal, be re-elected or re-appointed?

Just how much information should you share with a former client?

Antonym of "Megalomania"



How i change the retrofit URL at runtime


Retrofit 2 - Dynamic URLHow to save an Android Activity state using save instance state?How do I center text horizontally and vertically in a TextView?Why is the Android emulator so slow? How can we speed up the Android emulator?How can I open a URL in Android's web browser from my application?How do I fix android.os.NetworkOnMainThreadException?Moshi's Custom Adapter with RxAndroid & Retrofit & KotlinChanging request inside Retrofit CallAdapterRetrofit 2 login post request returns 500 but works well on postmanHow to Change API Base Url at Runtime(Retrofit,Android,Java)?Retrofit 2 - Getting response 200, but list is empty






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















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?










share|improve this question


























  • stackoverflow.com/questions/32559333/retrofit-2-dynamic-url

    – samaromku
    Mar 26 at 20:01

















3















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?










share|improve this question


























  • stackoverflow.com/questions/32559333/retrofit-2-dynamic-url

    – samaromku
    Mar 26 at 20:01













3












3








3








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?










share|improve this question
















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?







android kotlin retrofit2 koin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 19:33









Skizo-ozᴉʞS

10.8k13 gold badges47 silver badges111 bronze badges




10.8k13 gold badges47 silver badges111 bronze badges










asked Mar 26 at 19:24









Marcello SousaMarcello Sousa

214 bronze badges




214 bronze badges















  • stackoverflow.com/questions/32559333/retrofit-2-dynamic-url

    – samaromku
    Mar 26 at 20:01

















  • stackoverflow.com/questions/32559333/retrofit-2-dynamic-url

    – samaromku
    Mar 26 at 20:01
















stackoverflow.com/questions/32559333/retrofit-2-dynamic-url

– samaromku
Mar 26 at 20:01





stackoverflow.com/questions/32559333/retrofit-2-dynamic-url

– samaromku
Mar 26 at 20:01












2 Answers
2






active

oldest

votes


















0














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






share|improve this answer


































    0















    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.




    You can leave it as a baseUrl if you use @URL it will overwrite the one on yout Retrofit.Builder()



    You can use @URL parameter to change the endpoint dynamically.



    @GET
    fun getUsers(@Url String url) : Observable<UserResponse>





    share|improve this answer



























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364838%2fhow-i-change-the-retrofit-url-at-runtime%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      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






      share|improve this answer































        0














        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






        share|improve this answer





























          0












          0








          0







          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






          share|improve this answer















          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 26 at 21:15

























          answered Mar 26 at 21:09









          Dharma KshetriDharma Kshetri

          6,64711 gold badges40 silver badges77 bronze badges




          6,64711 gold badges40 silver badges77 bronze badges


























              0















              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.




              You can leave it as a baseUrl if you use @URL it will overwrite the one on yout Retrofit.Builder()



              You can use @URL parameter to change the endpoint dynamically.



              @GET
              fun getUsers(@Url String url) : Observable<UserResponse>





              share|improve this answer





























                0















                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.




                You can leave it as a baseUrl if you use @URL it will overwrite the one on yout Retrofit.Builder()



                You can use @URL parameter to change the endpoint dynamically.



                @GET
                fun getUsers(@Url String url) : Observable<UserResponse>





                share|improve this answer



























                  0












                  0








                  0








                  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.




                  You can leave it as a baseUrl if you use @URL it will overwrite the one on yout Retrofit.Builder()



                  You can use @URL parameter to change the endpoint dynamically.



                  @GET
                  fun getUsers(@Url String url) : Observable<UserResponse>





                  share|improve this answer














                  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.




                  You can leave it as a baseUrl if you use @URL it will overwrite the one on yout Retrofit.Builder()



                  You can use @URL parameter to change the endpoint dynamically.



                  @GET
                  fun getUsers(@Url String url) : Observable<UserResponse>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 26 at 19:30









                  Skizo-ozᴉʞSSkizo-ozᴉʞS

                  10.8k13 gold badges47 silver badges111 bronze badges




                  10.8k13 gold badges47 silver badges111 bronze badges






























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364838%2fhow-i-change-the-retrofit-url-at-runtime%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현