首页 > 解决方案 > 在进行 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方法中被调用。

标签: androidandroid-studioandroid-fragments

解决方案


我为我的问题找到了解决方案:显然,我的 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");

                    }
                });

推荐阅读