android - 在进行 REST-Api 调用时预期结果后获取 Null 列表
问题描述
我对使用 Android 和 Android Networking 还是很陌生,但我被卡住了。这是我的代码:
private void getParticipantsForMeeting(Integer id)
{
AndroidNetworking.get(LOCALHOST + "/meeting?meetingId={meetingid}")
.addPathParameter("meetingid", String.valueOf(meeting.getId()))
.setTag(this)
.setPriority(Priority.LOW)
.build()
.getAsObjectList(User.class, new ParsedRequestListener<List<User>>() {
@Override
public void onResponse(List<User> response) {
if (response == null)
Log.println(Log.INFO, "GETMEETINGPARTICIPANTS", "No participants found for this meeting");
else {
meeting.setParticipants(response);
Log.println(Log.INFO, "GETMEETINGPARTICIPANTS", "No participants found for this meeting");
}
}
@Override
public void onError(ANError anError) {
Log.println(Log.ERROR, "GETMEETINGPARTICIPANTS", "Error while trying to get participants for meeting");
}
});
}
当我在onResponse()
方法中时,我得到了请求的响应,但在解析方法后,meeting.getParticipants()
仍然为空。我不明白我做错了什么,因为其他电话都按预期工作,除了这个。我无法弄清楚这种奇怪的行为,我也尝试将响应添加到列表中并返回列表,将其另存为SharedPreferences
并保存在静态变量中,但问题仍然存在。也许相关的提及是该问题发生在片段中,并且其他调用未返回对象列表,而仅返回一个对象。有人可以解释一下为什么我会收到这个错误,我该如何解决?你真的会帮助一个水坑的学生!编辑:这是我的会议 POJO:
@Data
public class Meeting implements Serializable {
private Integer id;
private Long meetingDate;
private Boolean deleted;
private String location;
private MeetingType meetingType;
private String title;
private List<User> participants;
private String description;
}
我也尝试过获取 JSONArray,但我遇到了同样的问题:它在 onResponse 方法中可见,但在解析后不可见,这就是现在的样子:
AndroidNetworking.get(LOCALHOST + "/meeting?meetingId={meetingid}")
.addPathParameter("meetingid", String.valueOf(meeting.getId()))
.setTag(this)
.setPriority(Priority.LOW)
.build()
.getAsJSONArray(new JSONArrayRequestListener() {
@Override
public void onResponse(JSONArray response) {
if (response == null)
Log.println(Log.INFO, "GETMEETINGPARTICIPANTS", "No participants found for this meeting");
else {
JSONObject jsonObject;
Gson gson = new Gson();
for (int i=0; i<response.length();i++)
{
try {
jsonObject = response.getJSONObject(i);
User user = gson.fromJson(jsonObject.toString(), User.class);
users.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.println(Log.INFO, "GETMEETINGPARTICIPANTS", "No participants found for this meeting");
}
}
@Override
public void onError(ANError anError) {
Log.println(Log.ERROR, "GETMEETINGPARTICIPANTS", "Error while trying to get participants for meeting");
}
});
我的方法在onCreateView
方法中被调用。
解决方案
我为我的问题找到了解决方案:显然,我的 Rest 调用的方法仅在设置我的 TextViews 之后才被调用,即使它是在他们的设置器之前调用的。我认为这是由于异步调用造成的。我解决了在 onResponse 方法中为我的文本视图设置文本的问题。我不相信这是正确的解决方案,但目前这是一个很好的解决方法。
AndroidNetworking.get(LOCALHOST + "/meeting?meetingId={meetingid}")
.addPathParameter("meetingid", String.valueOf(id))
.setPriority(Priority.LOW)
.build()
.getAsJSONArray(new JSONArrayRequestListener() {
@Override
public void onResponse(JSONArray response) {
if (response == null)
Log.println(Log.INFO, "GETMEETINGPARTICIPANTS", "No participants found for this meeting");
else {
JSONObject jsonObject;
Gson gson = new Gson();
for (int i=0; i<response.length();i++)
{
try {
jsonObject = response.getJSONObject(i);
User user = gson.fromJson(jsonObject.toString(), User.class);
users.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
}
show_participants.setText(getParticipantsName(users)); //this worked for me
Log.println(Log.INFO, "GETMEETINGPARTICIPANTS", "No participants found for this meeting");
}
}
@Override
public void onError(ANError anError) {
Log.println(Log.ERROR, "GETMEETINGPARTICIPANTS", "Error while trying to get participants for meeting");
}
});
推荐阅读
- powerbi - 如何在滚动 3 个月时使用 percentile.exc 在 powerbi 中创建计算列?
- react-native - 在特定路线上按下时使标签导航按钮触发功能
- asp.net-core - 为什么 .net core 5 webapi xml 序列化程序与 swaggers 序列化程序不同?
- javascript - Eslint,prettier+airbnb react 风格指南,它不会根据我在 .prettierrc 中给出的规则进行格式化
- java - 我正在学习如何开发应用程序,但我不知道为什么我的应用程序崩溃了
- java - 有没有办法在 JavaFX 中简单刷新场景
- python - 从具有有序集和连接的字符串中删除停用词保留单个停用词
- css - Firefox 和 chrome 的自定义范围滑块剪辑不一致
- php - 如何向 TheGraph API 发出 PHP POST 请求?
- powershell - Powershell InvokeGlobal 参数计数不匹配