改造2-动态网址 [英] Retrofit 2 - Dynamic URL
问题描述
使用Retrofit 2,您可以在服务方法的注释中设置完整的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();
}
但是,在我的应用程序中,Web服务的URL在编译时未知,该应用程序在下载的文件中检索它们,因此我想知道如何将Retrofit 2与完整的动态URL结合使用.
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();
但是在这里,翻新没有看到该路径实际上是完整的URL,而是试图下载http://api.mysite.com/http%3A%2F%2Fapi.mysite.com%2Fuser%2Flist
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
关于如何将Retrofit与此类动态url一起使用的任何提示?
Any hint of how I could use Retrofit with such dynamic url ?
谢谢
推荐答案
我认为您使用的方式有误.以下是更改日志的摘录:
I think you are using it in wrong way. Here is an excerpt from the changelog:
新功能:@Url参数注释允许为端点传递完整的URL.
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);
}
这篇关于改造2-动态网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!