【问题标题】:Passing absolute URL to Retrofit将绝对 URL 传递给 Retrofit
【发布时间】:2020-11-30 02:22:59
【问题描述】:

是否可以在运行时动态传递绝对 url 进行改造?

例如,我访问了一个返回用户列表的 Rest API,并为分页提供了一个用于检索更多项目的绝对 url:

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

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

Retrofit https://github.com/square/retrofit/issues/333好像不支持

是否有一种仅适用于单个RestAdapter 的解决方法?

【问题讨论】:

  • 看起来 Retrofit 并不是为这个(非常简单的)用例而设计的。 Jake Wharton 在您链接的问题中的comment 似乎总结了这一点:“我真的,真的 不想支持这个”。例如 Volley 更适合获取绝对 URL。
  • @GET Call getUsers(@Url String url);见enter link description here

标签: android retrofit


【解决方案1】:

这似乎是可能的。除了将 url 传递给RestAdapter.Builder.setEndPoint,您还可以传递Endpoint 的子类,您可以使用它在每个请求之前动态设置您的 url。

【讨论】:

  • 但是如何使用@Path 等从绝对网址动态切换到“常规”网址。我只想拥有一个 RestAdapter。
  • Path 注释无法根据我所知动态更改。但是您可以通过使用动态 EndPoint 来实现相同的目的,正如我所描述的那样,它只指向您的 url。然后你可以简单地做一个没有@Path 的 GET(或任何你需要的方法)来获取你在 EndPoint 中设置的 url。使用这种方法只有一个 RestAdapter。
猜你喜欢
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 2023-03-19
  • 1970-01-01
相关资源
最近更新 更多