Search code examples
androidretrofitgithub-api

Why is retrofit reordering my QueryMap params?


I'm trying to query the github api like so...

https://api.github.com/search/repositories?q=created:>=2015-07-11&sort=stars&order=desc&perpage=25&page=1

Here is my GET

    @GET("/search")
    public void getTrending(@QueryMap Map<String,String> filters,Callback<GitResponse> response);

Here is how I setup my query Map...

    Map<String,String> searchFilters = new HashMap<>();
    searchFilters.put("created>","2015-07-11");
    searchFilters.put("sort","stars");
    searchFilters.put("order","desc");
    searchFilters.put("perpage","25");
    searchFilters.put("page", "1");


   api.getTrending(Collections.unmodifiableMap(searchFilters), new Callback<GitResponse>() {
       @Override
       public void success(GitResponse gitResponse, Response response) {
           Log.e("tag",response.toString());
       }

       @Override
       public void failure(RetrofitError error) {
           Log.e("tag",error.toString());
       }
   });

Here is the url that gets formatted by retrofit.

https://api.github.com/search/repositories?order=desc&created>=2015-07-11&perpage=25&sort=stars&page=1

Notice that the order of the params is not retained. I need the created >= to be the first param for the query to work correctly. I tried using Collections.unmodifiableMap() but that didn't help. In the retrofit docs, they are using guava's ImmutableMap.of(). Do I HAVE to use guava for the order to be retained?


Solution

  • The problem is not related to retrofit at all. You have a floating order because java.util.HashMap doesn't guarantee order. You should use LinkedHashMap instead to get fixed elements order.

    You better to know how hash map works by the way.