英文:
Calling Retrofit returns null
问题
所以,在我的项目中有很多模型类,并且有一个单例类,该类进行Retrofit调用以在模型类之间进行转换。该类中的getUsersFromChats
方法接受一个Chat
模型对象数组,将每个对象转换为User
模型对象,并将它们添加到公共的User
对象ArrayList中。
// 这些在另一个方法中实例化,并且不为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 {
// 处理错误响应
}
}
@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{
// 循环遍历clr.getChats()并执行相应操作
ConvertFieldsToUserObjects.getInstance(ChatActivity.this).getUsersFromChats(clr.getChats());
ArrayList<User> users = ConvertFieldsToUserObjects.users;
// 处理用户对象数组
}
}
}
@Override
public void onFailure(Call<ChatListResponse> call, Throwable t) {
// 处理失败
}
});
然而,这段代码不起作用,本地的ArrayList始终为空,即使从调用获取的Chat
对象列表包含元素。这是否与Retrofit调用是异步的有关,还是其他原因?(根据我测试的情况,API、RetrofitClients和Response对象都正常工作。)
英文:
So I have quite a few model classes in my project and have a singleton class which makes Retrofit calls to convert between the model classes. The method getUsersFromChats
in that class takes in an array of Chat
model objects, converts each object to a User
model object, and adds them to a public ArrayList of User
objects.
Code:
//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());
}
});
}
}
I have an Activity which calls that method after getting an array of Chat
objects from a Retrofit call and tries to save ArrayList from the singleton class to a local User
objects 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();
}
However, this code doesn't work and the local ArrayList is always empty even though the Chat
objects list obtained from the Call contains elements. Does this have something to do with the Retrofit calls being asynchronous or is it something else? (The API's, RetrofitClients, and the Response objects work fine as far as I have tested.)
专注分享java语言的经验与见解,让所有开发者获益!
评论