改造 2 呼叫覆盖

Retrofit 2 Calls Override

我在网上看到的关于改造 2 的所有示例都包含一个接口,该接口具有不同的 url 以进行调用。

我也做了同样的形式。

@GET("NewsFeed/latest")
Observable<ArrayList<News>> getNews(@Query("category") int category,
                                    @Query("language") int language,
                                    @Query("location") int location,
                                    @Query("poster") int poster,
                                    @Query("limit") int limit,
                                    @Query("offset") long offset);

该调用中的所有参数都是可选的,因此即使 none 的参数是 specified.Is 也可以进行调用,除了重载方法之外,我还有其他方法吗? 我应该使用@nullable 注释吗?

如您所见here,查询参数是可选的。通过将 null 传递给方法,改造会忽略查询参数。

(也许你不应该使用原始 类)

您可以使用 @QueryMap。这允许我们在 Map 中指定查询,并且可以轻松添加新的查询参数而无需修改现有代码。

@GET("NewsFeed/latest")
Observable<ArrayList<News>>getNews(
            @QueryMap Map<String, String> options);

用法:

private void fetchNews() {  
    Map<String, String> data = new HashMap<>();
    data.put("category", "Sports");
    data.put("language", String.valueOf(2));

    // simplified call
    newsService.getNews(data);
}

参考this for more details