Retrofit 2 - Dynamic URL
You can use the encoded flag on the @Path
annotation:
public interface APIService {
@GET("{fullUrl}")
Call<Users> getUsers(@Path(value = "fullUrl", encoded = true) String fullUrl);
}
- This will prevent the replacement of
/
with%2F
. - It will not save you from
?
being replaced by%3F
, however, so you still can't pass in dynamic query strings.
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();
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);
}
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);
}