在进行REST-Api调用时预期结果后获取Null列表

我对使用Android和Android网络很陌生,但遇到了麻烦。这是我的代码:

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");
                    }
                });

    }

when I am in onResponse() method, I get the requested response, but after parsing the method, meeting.getParticipants() is still null. I don't understand what I'm doing wrong, because other calls are working as expected, except this one. I can't figure out this strange behavior, I have also tried to add the response in a list and return the list, save it as SharedPreferences and save it in a static variable, but the problem is still here. Maybe a relevant mention is that issue happens in a fragment and the other calls didn't return a list of objects, but only one object. Could someone please explain me why I get this error and how can I fix it? You would really help a puddled student!

评论
  • lillo
    lillo 回复

    假设您与Json合作。那是你的问题。 “其他调用不返回对象列表,而仅返回一个对象。”您需要使用jsonarray,然后遍历数组并解析对象。 看这里,

    AndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAllUsers/{pageNumber}")
        .addPathParameter("pageNumber", "0")
        .addHeaders("token", "1234")
        .setPriority(Priority.LOW)
        .build()
        .getAsJSONArray(new JSONArrayRequestListener() {
        @Override
            public void onResponse(JSONArray response) {
                      // do anything with response
                    }