Android Retrofit - 如何覆盖baseUrl

j2e*_*nue 10 android retrofit

在改装适配器中,我已经为我的所有电话使用了基本URL.所以:

  Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com")
    .build();

GitHubService service = retrofit.create(GitHubService.class);
Run Code Online (Sandbox Code Playgroud)

让我们说上面是我的代码,我现在所有的调用都使用baseUrl.但是对于一个特定的调用,我想要更改基本URL,我不想为此创建另一个休息适配器,因为它仅用于本地测试.我可以更改界面中的终点可能不使用baseurl,或者是否有注释来提供我自己的基本URL?

Bla*_*elt 27

您可以使用@Url注释来提供完整的完整URL.例如

@GET
Call<GitHubUser> getUser(@Url String url);
Run Code Online (Sandbox Code Playgroud)


Zha*_*kov 14

在retrofi2中,@ GET中的路径会覆盖基本URL.

 @GET("https://someurl/api/supermarkets")
 Observable<List<TechIndex>> getTechIndexList();
Run Code Online (Sandbox Code Playgroud)

无论基本网址是什么,该请求都将发送到"someurl/api/..".希望能帮助到你