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;
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
add a comment |
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
stackoverflow.com/questions/32559333/retrofit-2-dynamic-url
– samaromku
Mar 26 at 20:01
add a comment |
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
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
android kotlin retrofit2 koin
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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>
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
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
add a comment |
add a comment |
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>
add a comment |
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>
add a comment |
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>
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>
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
stackoverflow.com/questions/32559333/retrofit-2-dynamic-url
– samaromku
Mar 26 at 20:01