1

Is it possible to pass an absolute url dynamically at runtime to retrofit?

For instance, I access a Rest API that returns a list of users and for pagination an absolute url that should be used to retrieve more items:

{
    "result": [
        {
            "id": 1,
            "name": "Daniel"
        },
        {
            "id": 2,
            "name": "Michael"
        },
        {
            "id": 3,
            "name": "Chris"
        }
    ],

    "pagination": "http://www.foo.com/users?offset=3"
}

It seems that it's not supported in Retrofit https://github.com/square/retrofit/issues/333

Is there a workaround that works with just one single RestAdapter?

Jonik
  • 80,077
  • 70
  • 264
  • 372
sockeqwe
  • 15,574
  • 24
  • 88
  • 144
  • Looks like Retrofit just isn't made for this (very simple) use case. Jake Wharton's [comment](https://github.com/square/retrofit/issues/333#issuecomment-25635325) in the issue you linked seems to sum it up: "I really, *really* wouldn't like to support this". For example Volley is better suited to getting absolute URLs. – Jonik May 11 '15 at 15:29
  • Possible duplicate of [Using absolute URLs with Retrofit](http://stackoverflow.com/questions/28116395/using-absolute-urls-with-retrofit) – kotucz Nov 13 '15 at 13:14
  • @GET Call getUsers(@Url String url); see [enter link description here](http://stackoverflow.com/questions/25400897/any-way-to-have-generic-url-using-retrofit?noredirect=1&lq=1) – Fan Applelin Feb 21 '17 at 06:54

1 Answers1

0

It seems this is possible. Instead of passing an url to the RestAdapter.Builder.setEndPoint, you can pass a subclass of Endpoint, which you can use to dynamically set your url before every request.

Alex Kolpa
  • 187
  • 1
  • 4
  • But how do I switch dynamically from a absolute url to an "usual" url with @Path etc. I only want to have one single RestAdapter. – sockeqwe Feb 21 '15 at 13:48
  • the Path annotation can't be changed dynamically from what I can tell. But you can achieve the same by using a dynamic EndPoint as I described which just points to your url. Then you can simply do a GET (or whatever method you need) without a @Path to get the url you set in your EndPoint. Using this method there is only one RestAdapter. – Alex Kolpa Feb 21 '15 at 13:58