pdegand59 pdegand59 - 1 month ago 39
Android Question

Retrofit 2 - Dynamic 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

Answer

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);
}