Android改造GET @Query ArrayList [英] Android Retrofit GET @Query ArrayList

查看:91
本文介绍了Android改造GET @Query ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一个GET API,该API期望在其查询参数中列出一个字符串数组.

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.

在API接口中,我定义了以下内容:

In the API interface, I have the following defined:

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

我正在使用Timber进行日志记录,并且在控制台中,我看到发出的请求如下:

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]

尽管类别是字符串arraylist类型,为什么要进行这种方式的改装?

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

推荐答案

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

已生成最终请求

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

这篇关于Android改造GET @Query ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆