android - 如何使用 Retrofit 2 从 JSON 列表中获取 List<String>?

标签 android json retrofit

我是 Android 新手。我需要从 JSON 列表中获取字符串列表。它将被添加到 Spinner 列表中。我在服务器上有这样的列表

[{"bgroup":"A+"},{"bgroup":"A1+"},{"bgroup":"A1-"}]. 

尝试使用 Retrofit Scalars。 响应为

[{"bgroup":"A+"},{"bgroup":"A1+"},{"bgroup":"A1-"}]

但错误检测为:

Expected a string but was BEGIN_OBJECT at line 1 column 3 path $[0]*

有没有更好的方法从 JSON 中检索字符串列表?

最佳答案

这是示例代码:

 GroupService groupService = createService(GroupService.class);

 Call<List<Groups>> groupCall = groupService.getGroups();

    groupCall.enqueue(new Callback<List<Groups>>() {
            @Override
            public void onResponse(retrofit.Response<List<Groups>> response, Retrofit retrofit) {


            }

            @Override
            public void onFailure(Throwable t) {
                t.printStackTrace();

            }
        });

接口(interface):

public interface GroupService {

@GET("<URL>")
Call<List<Groups>> getGroups();
}

同时创建模型名称组。

希望对您有所帮助。

关于android - 如何使用 Retrofit 2 从 JSON 列表中获取 List<String>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44363068/

相关文章:

android - onCreateOptionsMenu 永远不会被调用

java - 我什么时候必须沮丧一些事情?

javascript - 数据通过 websocketpp 返回为 json,但通过 messagepack 返回为 Blob

json - 如何使用 Nickel 模板返回 JSON 文件?

android - 使用 Retrofit2 的 HTTP 请求得到 "CLEARTEXT communication not supported"错误

android - 限制 Aviary 功能

android - 带有 NULLABLE 外键的 SQLite

php - 通过 JSON 将 HTML 从 PHP 发送到 javascript

android - Android 6 上的 Retrofit + Robospice native 崩溃 - 致命信号 11 (SIGSEGV),代码 1

android - retrofit - I/art : Background sticky concurrent mark sweep GC freed