我正在尝试从 API 检索一些数据,但我总是在异步任务中获取 null。这是我的异步任务:
private class DownloadTask extends AsyncTask<Bundle, Void, List<Topic>> {
@Override
protected void onPreExecute() {
HomeActivity.mProgressBar.setVisibility(View.VISIBLE);
HomeActivity.mProgressBar.setIndeterminate(true);
}
@Override
protected List<Topic> doInBackground(Bundle... params) {
return downloadPhotos(params[0]);
}
@Override
protected void onPostExecute(List<Topic> topics) {
HomeActivity.mProgressBar.setVisibility(View.INVISIBLE);
HomeActivity.mProgressBar.setIndeterminate(false);
Log.d("List Size: ", ""+topics); // 0
adapter = new TopicListAdapter(activity, topics);
RecyclerView.LayoutManager manager = new MyCustomLayoutManager(activity);
recyclerView.setLayoutManager(manager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
}
}
检索数据的方法应该将两个数组合并为一个数组,因为我要从两个位置检索数据:
private List<Topic> downloadPhotos(Bundle params) {
String profileId = activity.getPreferencesManager().getProfileId();
List<Topic> topicsFromMe, topicsFromFriends;
topicsFromFriends = setValuesFromFriends(params);
topicsFromMe = setValuesFromMe(profileId, params);
topicsFromFriends.addAll(topicsFromMe);
sortTopics(topicsFromFriends);
int k = topicsFromFriends.size();
Log.d("List Size: ", "" + topicsFromFriends); // here also 0 for size
if (k > 10)
topicsFromFriends.subList(10, k).clear();
return topicsFromFriends;
}
这是我将值设置为数组列表的一种方法。奇怪的是,在这种情况下,RecyclerView 填充了这个数组,但我没有得到我想要的结果。例如,我应该对此列表进行排序并仅显示其中的 10 条记录。
private List<Topic> setValuesFromFriends(final Bundle params) {
final List<Topic> topics = new ArrayList<>();
activity.getSimpleFacebook().getFriends(new OnFriendsListener() {
@Override
public void onComplete(List<Profile> friends) {
for (final Profile profile : friends) {
activity.getSimpleFacebook().get(profile.getId(), "photos/uploaded", params,
new OnActionListener<List<Photo>>() {
@Override
public void onComplete(List<Photo> photos) {
for (final Photo photo : photos) {
// Initialize instance of Topic
final User user = photo.getFrom();
final Topic topic = new Topic();
topic.setCaption(photo.getName());
topic.setImageId(photo.getId());
topic.setCreatedTime(photo.getCreatedTime());
topic.setPostImage(photo.getSource());
topic.setUserId(user.getId());
topic.setName(user.getName());
final Bundle likeParams = new Bundle();
likeParams.putString("fields", "total_count");
likeParams.putString("limit", "100000");
activity.getSimpleFacebook().get(photo.getId(), "likes",
likeParams, new OnActionListener<List<Like>>() {
@Override
public void onComplete(List<Like> likes) {
topic.setNumOfLikes(likes.size());
topics.add(topic);
}
@Override
public void onThinking() {
super.onThinking();
}
});
}
}
});
}
}
});
return topics;
}
最佳答案
您错误地使用了 AsyncTask。
AsyncTask 正在启动另一个线程 (thread1),并在其中执行方法 doenloadPhotos
。此方法正在调用 setValuesFromFriends
,该方法使用 getFriends
方法创建另一个线程 (thread2)。当 thread2 启动后,setValuesFromFriends
中的其余代码将被执行。
它的工作原理如下:
private List<Topic> setValuesFromFriends(final Bundle params) {
final List<Topic> topics = new ArrayList<>();
//launched process on new thread
return topics; //this is 0 as topics = new ArrayList<>();
}
现在topicsFromFriends = 0
。因此,您得到的输出 = 0。
实际上,线程 1 是在线程 2 完成之前执行的。由于thread1的输出为0,所以onPostExecute
不需要使用AsyncTask。
您应该将所有必需的代码放入 new OnFriendsListener()
的 onComplete
中。这样信息就会正确显示。您可以在 setValuesFromFriends
之前启动进度条,然后在 onComplete
中将其删除。
关于java - 具有对象 ArrayList 结果的 AsyncTask 在 OnPostExecute 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39823181/