调用Retrofit返回null

问题描述

因此,我的项目中有很多模型类,并且有一个单例类,该类使Retrofit调用在模型类之间进行转换。该类中的方法getUsersFromChats接收一个Chat模型对象的数组,将每个对象转换为一个User模型对象,并将它们添加到User对象的公共ArrayList中

代码:

//These are instantiated in another method and are not null.
public static ArrayList<User> users;
private static int index;

public void getUsersFromChats(Chat[] chats) {

        if (index < chats.length) {

            Call<UserResponse> getUser = AuthRetrofitClient
                    .getInstance()
                    .getAuthApi()
                    .getUserById(chats[index].getUserID());


            getUser.enqueue(new Callback<UserResponse>() {
                @Override
                public void onResponse(Call<UserResponse> call,Response<UserResponse> response) {

                    if (response.isSuccessful()) {

                        UserResponse ur = response.body();
                        Log.i("User",ur.getUser().getName());
                        users.add(ur.getUser());
                        Log.i("List",users.toString());

                        index++;
                        getUsersFromChats(chats);

                    } else {
                        try {
                            String errorBody = response.errorBody().string();
                            int index = errorBody.indexOf("\"message\":");

                            if (index != -1) {

                                String errorSub = errorBody.substring(index + 10);
                                //errorBody = errorSub.substring(1,errorSub.length() - 2);
                            }

                            Toast.makeText(context,errorBody,Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                @Override
                public void onFailure(Call<UserResponse> call,Throwable t) {
                    Log.i("Didn't work",t.toString());
                }
            });
        }
    }

我有一个Activity,在从Retrofit调用中获取Chat个对象的数组后调用该方法,并尝试将ArrayList从单例类保存到本地User个对象ArrayList。

Call<ChatListResponse> getAllContacts = ChatRetrofitClient
                    .getInstance()
                    .getChatAPI()
                    .loadChats(SharedPrefManager.getInstance(ChatActivity.this).getUser().getToken());

            getAllContacts.enqueue(new Callback<ChatListResponse>() {
                @Override
                public void onResponse(Call<ChatListResponse> call,Response<ChatListResponse> response) {

                    if(response.isSuccessful()){

                        ChatListResponse clr = response.body();

                        if(clr.getChats() == null || clr.getChats().length < 1)
                            createChat();

                        else{

                            for(Chat c: clr.getChats())
                                Log.i("Testing",c.getUserID());

                            ConvertFieldsToUserObjects.getInstance(ChatActivity.this).getUsersFromChats(clr.getChats());
                            ArrayList<User> users = ConvertFieldsToUserObjects.users;

                            Log.i("Testing",users.toString());

                            for(User u: users)
                                Log.i("Testing",u.getId());

                            Log.i("Testing",person.getId());

                            if(users.contains(person)){

                                int i = users.indexOf(person);
                                chat = clr.getChats()[i];
                                Toast.makeText(ChatActivity.this,chat.getName(),Toast.LENGTH_SHORT).show();
                            }

但是,即使从Call获得的Chat对象列表包含元素,此代码也无法工作,并且本地ArrayList始终为空。这与Retrofit调用异步有关吗,还是其他原因? (据我测试,API,RetrofitClients和Response对象都可以正常工作。)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...