Android Retrofit GET @Query ArrayList

人走茶凉 提交于 2019-12-24 04:51:12

问题


I am trying to consume a GET API that expects in its query params list an array of strings.

I am using Retrofit for the task.

In the API interface, I have the following defined:

@GET("user/explore")
Observable<User> fetchUsers(@Query("who") String who,
                                                        @Query("category") ArrayList<String> categories);

I am using Timber for logging and in the console, I see the request is made like the following:

http://192.168.2.208:3000/api/v1/user/explore?who=SOME_USER&category=SOME_CATEGORY

The expected request should be made like the following:

http://192.168.2.208:3000/api/v1/user/explore?who=SOME_USER&category=[SOME_CATEGORY,SOME_CATEGORY1,SOME_CATEGORY2]

Why is retrofit behaving this way event though categories are of type string arraylist?


回答1:


ArrayList<String> carArrList = new ArrayList<>();
        carArrList.add("a");
        carArrList.add("b");
        carArrList.add("c");
        String[] catArr = (String[]) carArrList.toArray(new String[carArrList.size()]);
        String catStr = Arrays.toString(catArr);

        catStr = catStr.replaceAll(" ", "");
        Log.e("catStr", catStr);
        Call<User> call = apiService.fetchUsers(catStr);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
            }
        });



@GET("list/")
    Call<User> fetchUsers(@Query("category") String categories);

final request generated

list/?category=[a,b,c]


来源:https://stackoverflow.com/questions/44887212/android-retrofit-get-query-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!