我是 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/