Retrofit 2 - Dynamic URL

dagger retrofit dynamic base url
retrofit url parameters
how to change base url in retrofit android
pass parameter in url retrofit
retrofit multiple endpoints
retrofit2
retrofit get data from url
retrofit get request url

With Retrofit 2, you can set a full URL in the annotation of a service method like :

public interface APIService {
  @GET("http://api.mysite.com/user/list")
  Call<Users> getUsers();
}

However, in my app, the URL of my webservices are not known at compile time, the app retrieves them in a downloaded file so i'm wondering how i can use Retrofit 2 with full dynamic URL.

I tried to set a full path like :

public interface APIService {
  @GET("{fullUrl}")
  Call<Users> getUsers(@Path("fullUrl") fullUrl);
}

new Retrofit.Builder()
  .baseUrl("http://api.mysite.com/")
  .build()
  .create(APIService.class)
  .getUsers("http://api.mysite.com/user/list"); // this url should be dynamic
  .execute();

But here, Retrofit doesn't see that the path is actually a full URL and is trying to download http://api.mysite.com/http%3A%2F%2Fapi.mysite.com%2Fuser%2Flist

Any hint of how I could use Retrofit with such dynamic url ?

Thank you

I think you are using it in wrong way. Here is an excerpt from the changelog:

New: @Url parameter annotation allows passing a complete URL for an endpoint.

So your interface should be like this:

public interface APIService {
    @GET
    Call<Users> getUsers(@Url String url);
}

Retrofit 2 - Dynamic URL, I think you are using it in wrong way. Here is an excerpt from the changelog: New: @Url parameter annotation allows passing a complete URL for an endpoint. Retrofit 2 finally comes with an additional annotation for dynamic urls. What was a hackaround in Retrofit 1 is now just a single @Url annotation for the endpoint declaration. This post will show you how to make use of dynamic endpoint urls for individual requests.

I wanted to replace only a part of the url, and with this solution, I don't have to pass the whole url, just the dynamic part:

public interface APIService {

  @GET("users/{user_id}/playlists")
  Call<List<Playlist> getUserPlaylists(@Path(value = "user_id", encoded = true) String userId);
}

How to use dynamic urls at Runtime with Retrofit 2?, This article about handle dynamic urls with Retrofit 2. Almost all applications include this library and for some cases we need to change url at runtime or need​  Dynamic Urls or Passing a Full Url. With Retrofit 2, you’ve the ability to pass a given url to an endpoint which then is used for the request. If you want to read more about dynamic urls in Retrofit 2, there’s another post waiting for you :) When using a dynamic url, Retrofit is very flexible and can keep your scheme from the base url.

Retrofit 2 - Dynamic URL, With Retrofit 2 you can set a full URL in the annotation of a service method like public interface APIService GEThttpapimysitecomuser Retrofit 2 — How to Change API Base Url at Runtime by Norman Peitek on April 07 2016 , tagged in Android , Retrofit , 8 min read

As of Retrofit 2.0.0-beta2, if you have a service responding JSON from this URL : http://myhost/mypath

The following is not working :

public interface ClientService {
    @GET("")
    Call<List<Client>> getClientList();
}

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://myhost/mypath")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

ClientService service = retrofit.create(ClientService.class);

Response<List<Client>> response = service.getClientList().execute();

But this is ok :

public interface ClientService {
    @GET
    Call<List<Client>> getClientList(@Url String anEmptyString);
}

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://myhost/mypath")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

ClientService service = retrofit.create(ClientService.class);

Response<List<Client>> response = service.getClientList("").execute();

Retrofit Tutorial, In this video you'll learn how Retrofit can use dynamic URLs with @Url. Tip: turn on Duration: 4:15 Posted: Apr 6, 2017 In part 2 of the Retrofit tutorial, we will learn how to use replacement blocks and query parameters to access more dynamic URL endpoints of a REST API. Instead of hardcoding the relative URL into our GET requests, we can use curly braces to create placeholders, which we can then replace at runtime with arguments that we pass to the method and annotated with @Path.

You can use this :

@GET("group/{id}/users")

Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort);

For more information see documentation https://square.github.io/retrofit/

Url (Retrofit 2.7.1 API), retrofit2.http @GET Call<ResponseBody> list(@Url String url);. See base URL for details of how the value will be resolved against a base URL to create the  I am new with Retrofit 2, and am trying to integrate a Google Place API in my App. My Question is how to move forward with this kind of Dynamic URL while using Retrofit 2.0. URL: https://maps.

Retrofit, Retrofit turns your HTTP API into a Java interface. A request URL can be updated dynamically using replacement blocks and parameters on the method. Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars. The second option is new to Retrofit 2. You can now easily pass a dynamic value as full URL to the request call. This can be especially helpful when downloading files, which are dependent of a parameter, user or time. You can build the URL during runtime and request the exact file without any hacks.

Android Dagger 2.11 and Retrofit dynamic URLs : androiddev, 8 points · 2 years ago · edited 2 years ago. Hey for your dynamic url problem here is the fix, no need for an interceptor: /** * This is your retrofit interface  Retrofit is the class through which your API interfaces are turned into callable objects. By default, Retrofit will give you sane defaults for your platform but it allows for customization. Converters. By default, Retrofit can only deserialize HTTP bodies into OkHttp's ResponseBody type and it can only accept its RequestBody type for @Body.

retrofit-dynamic-url · GitHub Topics · GitHub, Let Retrofit support multiple dynamic baseUrl and can be change the baseUrl at runtime. retrofit2 okhttp3 retrofit-dynamic-url okhttp-interceptor. Dynamic Paths in Retrofit. Ask Question Asked 5 years, 10 months ago. Retrofit 2 - Dynamic URL. 0. Dynamic endpoints using Retrofit Get Method. Hot Network Questions

Comments
  • For future searchers, there is a guide on dynamic URLs with Retrofit: futurestud.io/tutorials/…
  • @pdegand59 It's brand new :) see changelog
  • Why baseurl is required though the url for the endpoint is a fullurl?
  • If I use this @Url annotation. Then What should we write in .baseUrl() ??
  • you should write your primary url. @Url parameter will override it anyway.
  • I tried adding the URL like the one above but the baseUrl that i normally set for the retrofit builder overides the full url i specified in the parameter so instead of it being dynamic.com it gets translated to MyBaseUrl/http://dynamic.com
  • helped me a lot ... Thanks
  • It is not a typo.
  • and what to do to replace %3F with ?