2017-02-26 58 views
0

我在网上看到的关于改装2的所有示例都包含一个界面,该界面具有用于调用的不同网址。改装2调用覆盖

我已经做了相同的形式。

@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); 

所有在调用的参数都是可选的,因此呼叫可以进行,即使没有任何参数都specified.Is有没有办法,我可以做的是,除了重载方法? 我应该使用@nullable注释吗?

回答

1

您可以使用@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

0

正如你可以看到here,查询参数是可选的。通过将null传递给方法,retrofit将忽略查询参数。

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