使用Retrofit 2,您可以在服务方法的注释中设置完整的URL,如:
public interface APIService {
@GET("http://api.mysite.com/user/list")
Call<Users> getUsers();
}
Run Code Online (Sandbox Code Playgroud)
但是,在我的应用程序中,我的webservices的URL在编译时是未知的,应用程序在下载的文件中检索它们,所以我想知道如何使用Retrofit 2和完整的动态URL.
我试图设置一个完整的路径,如:
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();
Run Code Online (Sandbox Code Playgroud)
但在这里,Retrofit并未发现该路径实际上是一个完整的URL并且正在尝试下载 http://api.mysite.com/http%3A%2F%2Fapi.mysite.com%2Fuser%2Flist
有关如何使用这种动态网址进行Retrofit的任何提示吗?
谢谢
我正在使用Retrofit访问RESTful api.基本网址是:
这是界面的代码:
public interface ExampleService {
@Headers("Accept: Application/JSON")
@POST("/album/featured-albums")
Call<List<Album>> listFeaturedAlbums();
}
Run Code Online (Sandbox Code Playgroud)
这就是我发送请求并接收响应的方式:
new AsyncTask<Void, Void, Response<List<Album>>>() {
@Override
protected Response<List<Album>> doInBackground(Void... params) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.example.com/service")
.addConverterFactory(GsonConverterFactory.create())
.build();
ExampleService service = retrofit.create(ExampleService.class);
try {
return service.listFeaturedAlbums().execute();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Response<List<Album>> listCall) {
Log.v("Example", listCall.raw().toString());
}
}.execute();
Run Code Online (Sandbox Code Playgroud)
我得到的日志是奇怪的事情:
V /示例:响应{protocol = http/1.1,代码= 404,消息=未找到,url = http://api.example.com/album/featured-albums }
这里发生了什么?