数据未添加到列表。该程序从GraphQL服务器获取数据并通过switch语句对其进行过滤,然后将其附加到相应的列表中。但是,如果在switch语句外部访问,则该列表为空。如果在switch语句中打印列表之一,则将正确打印,但是如果在getter函数中打印,则将不会返回任何内容。
这有什么问题?范围?我尝试将初始化放在相同功能或构造函数的几个位置,但结果相同。
package sukebei.anilog.data.source.AniList;
import android.util.Log;
import com.apollographql.apollo.ApolloCall;
import com.apollographql.apollo.ApolloClient;
import com.apollographql.apollo.api.Response;
import com.apollographql.apollo.exception.ApolloException;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import okhttp3.OkHttpClient;
import AniList.UserMediaQuery;
import AniList.UserMediaQuery.Data;
import AniList.type.MediaType;
/* Data Received Example
[
Entry {
__typename = MediaList,
progress = 0,
progressVolumes = null,
score = 0.0,
status = PLANNING,
notes = null,
repeat = 0,
media = Media {
__typename = Media,
chapters = null,
volumes = null,
idMal = 17082,
episodes = 12,
title = Title {
__typename = MediaTitle, romaji = Aiura
}
}
},
Entry {
__typename = MediaList,
progress = 0,
progressVolumes = null,
score = 0.0,
status = PLANNING,
notes = null,
repeat = 0,
media = Media {
__typename = Media,
chapters = null,
volumes = null,
idMal = 33218,
episodes = 1,
title = Title {
__typename = MediaTitle,
romaji = Kimi no Koe wo Todoketai
}
}
}
]
*/
public class AniList {
private static final String BASE_URL = "https://graphql.anilist.co";
private List<UserMediaQuery.Entry> planningMedia = new ArrayList<UserMediaQuery.Entry>();
private List<UserMediaQuery.Entry> currentMedia = new ArrayList<UserMediaQuery.Entry>();
private List<UserMediaQuery.Entry> completedMedia = new ArrayList<UserMediaQuery.Entry>();
private List<UserMediaQuery.Entry> droppedMedia = new ArrayList<UserMediaQuery.Entry>();
private List<UserMediaQuery.Entry> pausedMedia = new ArrayList<UserMediaQuery.Entry>();
private List<UserMediaQuery.Entry> repeatingMedia = new ArrayList<UserMediaQuery.Entry>();
private OkHttpClient okHttpClient;
private ApolloClient apolloClient;
public AniList() {
okHttpClient = new OkHttpClient.Builder().build();
apolloClient = ApolloClient.builder()
.serverUrl(BASE_URL)
.okHttpClient(okHttpClient)
.build();
}
public void loadUserData(String username, MediaType mediaType) {
UserMediaQuery mediaQuery = UserMediaQuery.builder()
.username(username)
.type(mediaType)
.build();
apolloClient.query(mediaQuery).enqueue(new ApolloCall.Callback<Data>() {
@Override public void onResponse(@NotNull Response<Data> dataResponse) {
for (UserMediaQuery.List data : dataResponse.data().userMediaList().lists()) {
for (UserMediaQuery.Entry entry: data.entries()) {
switch(entry.status()) {
case PLANNING:
planningMedia.add(entry);
break;
case CURRENT:
currentMedia.add(entry);
break;
case COMPLETED:
completedMedia.add(entry);
break;
case DROPPED:
droppedMedia.add(entry);
break;
case PAUSED:
pausedMedia.add(entry);
break;
case REPEATING:
repeatingMedia.add(entry);
break;
}
}
}
}
@Override
public void onFailure(@NotNull ApolloException t) {
Log.e("AniList Source", t.getMessage(), t);
}
}
);
}
public List getPlanningMedia() {
return planningMedia;
}
public List getCurrentMedia() {
return currentMedia;
}
public List getCompletedMedia() {
return completedMedia;
}
public List getDroppedMedia() {
return droppedMedia;
}
public List getPausedMedia() {
return pausedMedia;
}
public List getRepeatingMedia() {
return repeatingMedia;
}
}
这会打印数据,但是当您在getter函数中打印数据时,它不会打印数据。
for (UserMediaQuery.List data : dataResponse.data().userMediaList().lists()) {
for (UserMediaQuery.Entry entry: data.entries()) {
if (entry.status() == MediaListStatus.PLANNING) {
planningMedia.add(entry);
Log.d("planning", planningMedia.toString());
}
}
}
答案 0 :(得分:0)
apolloClient.query(mediaQuery).enqueue
以异步方式执行代码,这意味着它使请求入队,因此数组列表将为空,直到请求完成并从另一个线程成功为止。
您可能直接使用getter而不等待回调完成。如果是这种情况,请检查以下可能的解决方案。
如果您正在等待请求完成以在UI上显示某些内容,则可能希望通过使用notifyDataSetChanged()
之类的东西将数据再次绑定到UI,例如在RecyclerView
的情况下。同样,如果要将这些数据用于其他用途,则应该使用回调机制。
希望这会有所帮助!