java - Android:使用带有动态 url 的 Retrofit

标签 java android retrofit

我有一个类似于 http://www.myexample.com/rss/choose.php?type=level&id=2 的网址,并且我想在我的 Android 应用程序中使用 Retrofit。问题是 type 是一个参数,它可以采用不同的值,例如 levelgrade 等。 我无法在每次 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 在界面上typeid 并在 start() 中调用它时发送值:

@GET
Call<Channel> loadChannel(@Query("type") String type,
                          @Query("id") String value);

关于java - Android:使用带有动态 url 的 Retrofit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47109512/

相关文章:

android - 如何模拟 Retrofit Response

java - Android 使用 Retrofit 和 OkHttp 获取 Endpoints

android - Retrofit 从基本 url 中删除查询参数

java - 有没有办法在 Android Studio 中标记 TODO 完成?

java - Excel 公式不更新单元格

java - Spark join/groupby 数据集需要很多时间

java - 从 Sun 应用程序服务器中删除 server.policy

android - Android UI 中类似 Excel 的行为

java - 查找数据库结构中子节点的数量

java - Java中如何读写串口通信?