gpt4 book ai didi

java - 改造预计为 BEGIN_ARRAY,但实际为 BEGIN_OBJECT

转载 作者:行者123 更新时间:2023-12-01 09:30:39 27 4
gpt4 key购买 nike

我正在努力通过改造和 RXAndroid 从 Android 与我的 REST API 进行交互,但遇到了问题。

当我向“api/users/{id}/{id}/classes”发送 GET 请求时,我的 api 会返回用户类列表,如下所示:

{
"classes": [
{
"assignments": [
{
"asstype": 0,
"classs_id": 1,
"desc": "Read",
"due": "9/14/16",
"id": 1
}
],
"badge": 0,
"days": "W",
"id": 1,
"location": "W.Claire Harris Textiles 136A",
"name": "STS 1101",
"professor": "Stickel",
"time": "5:00 - 6:15",
"user_id": 1
},
{
"assignments": [],
"badge": 1,
"days": "TR",
"id": 2,
"location": "Atrium 201L",
"name": "HIST 2111",
"professor": "Pierce",
"time": "6:30 - 7:45",
"user_id": 1
}
]
}

我的界面如下所示:

@GET("api/users/{user_id}/classes")
Ovservable<List<Clazz>> getClasses(@Header("Authorization") String auth, @Path("user_id")int user_id);

我从我的 fragment 中这样调用它:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_classes, container, false);
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(Sched.API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

RecyclerView classesRV = (RecyclerView)rootView.findViewById(R.id.classRec);
ClassInterface classAPI = retrofit.create(ClassInterface.class);
Observable<List<Class>> classes = classAPI.getClasses("Basic " +Base64.encodeToString("joyod3@gmail.com:nightman_420".getBytes(), Base64.NO_WRAP), 1);

classes.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<Class>>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {
e.printStackTrace();
}

@Override
public void onNext(List<Class> classes) {
classesRV.setAdapter(new ClassRecyclerViewAdapter(classes, getContext()));
classesRV.setLayoutManager(new LinearLayoutManager(getContext()));
}
});
return rootView;
}

我的 Class 对象与 getter 和 setter 看起来像这样:

public class Class {
private int id;
private String name;
private int badge;
private String professor;
private String location;
private String days;
private String time;
private int user_id;
private List<Assignment> assignments;

public Class() {
}

gson 不应该看到 "classes": [] 数组并从中解析对象吗?我做错了什么

最佳答案

正如 @CommonsWare 所提到的,使用此 JSON 响应结构,您将需要一些中间类来保存 Clazz 的集合。 s (或者完全编写一个自定义反序列化器)。像这样:

public class ClassHolder {

List<Clazz> classes;

}

getClasses()的返回类型将更改为 Observable<ClassHolder>相应地。

关于java - 改造预计为 BEGIN_ARRAY,但实际为 BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39441663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com