我有一个类似于 http://www.myexample.com/rss/choose.php?type=level&id=2
的网址,并且我想在我的 Android 应用程序中使用 Retrofit。问题是 type
是一个参数,它可以采用不同的值,例如 level
、grade
等。
我无法在每次 type
更改时调整 Retrofit,因为我在 TestesInterface 中收到“此处不允许使用注释”。我正在寻找这个post并应用于我的应用程序,但它不起作用。
static final String BASE_URL = "http://www.myexample.com/rss/";
public void start() {
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(SimpleXmlConverterFactory.create()).build();
TestesInterface testesInterface = retrofit.create(TestesInterface.class);
Call<Channel> call = testesInterface.loadChannel("choose.php?type=level&id=2");
call.enqueue(this);
}
我的界面:
public interface TestesInterface {
@GET
Call<Channel> loadChannel(@Url type);
}
这使得每次我想将类型更改为不同的值时,我都应该更改 testesInterface.loadChannel("choose.php?type=level&id=2")
。这是行不通的。你能帮我吗?
最佳答案
尝试不同的方法后我得到了解决方案。
我必须使用标签 @Query
在界面上type
和 id
并在 start() 中调用它时发送值:
@GET
Call<Channel> loadChannel(@Query("type") String type,
@Query("id") String value);
关于java - Android:使用带有动态 url 的 Retrofit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47109512/