您的位置:首页 > 移动开发

如何在App中实现朋友圈功能之八页面加载功能的逻辑与实现——箭扣科技Arrownock

2015-12-02 12:07 671 查看

如何在App中实现朋友圈功能

之八

页面加载功能的逻辑与实现

本文将给大家带来的是本系列最难的一部分功能——查询。查询功能从技术上可以分为三部分:1.进入页面加载;2.下拉刷新加载;3.上拉刷新加载。本文我们将主要讲解进入页面加载功能的逻辑与实现。 实现逻辑从数据库加载出limit(比如10)条postList,如果有Post,再根据每一个postId从数据库拿取Comment和Like,然后刷新页面。数据库没有Post,访问服务器获取limit条Post:a. 如果服务器返回了Post数据,则将这些Post存入数据库,接着根据每一个postId从服务器查询出Comment和Like,加载出来的数据存入数据库,等全部加载完毕,刷新界面。b. 如果服务器没有返回Post数据,则在界面显示目前没有朋友圈消息。递归技巧 查询Post,使用posts/query.json 参数:wall_id //墙Iduser_id //用户的所有好友id,多个好友id用”,”分割sort=-created_at //根据Post的创建时间倒序排序,取最新的Postlimit //自定义取多少条数据查询Comment和Like,使用comments/query.json和Likes/query.json参数: object_type=Post //对象类型,固定Postobject_id //postIduser_id //用户的所有好友id,多个好友id用”,”分割end_time //到目标时间为止的所有数据 这里在查询Comment和Like的时候有个递归查询出所有数据的技巧,我们给出示例代码(以Android代码为例):
// 根据postId循环从服务器获取like
private void queryLikeByPostId(final Post post, int limit) {
// 根据当前postId拿到数据库加载queryLike的params
// 注意:需要使用最近一条like数据的created_at作为end_time条件查询,
// 如果不存在like数据则不使用该条件
Map<String, Object>params = loadQueryLikeParamsByPostId(post, limit);
try {
Map<String,Object> params = loadQueryLikeParamsByPostId(postId);
anSocial.sendRequest("likes/query.json", AnSocialMethod.GET,
params, new IAnSocialCallback() {
@Override
public void onSuccess(JSONObject response) {
try {
// 存储数据到本地数据库
saveLikeToDB(response);
JSONObjectmeta = response.getJSONObject("meta");
int total =meta.getInt("total");
// 判断服务器是否还有更多数据
if (total > limit){
// 取剩余的数据
queryLikeByPostId(post, total - limit);
} else {
// 刷新界面
refreshUIView();
}
} catch (JSONExceptione) {
}
}
@Override
public void onFailure(JSONObject response) {
}
});
} catch(ArrownockException e) {
}
}


Comment与此类似,实现方式:
private void initPost() {
// 先从数据库得post
postList = PostDBHelper.getAllPosts(POST_LIMIT, 0);
// 如果有post
if (null != postList && !postList.isEmpty()) {
// 则根据postId从数据库得到comment和like
LoadPostListLikeAndComment();
// 刷新
refreshUIView();
}
// 如果数据库没有post,则从服务器获取post
else {
Map<String,Object> params = loadInitPostParams();
try {
mTA.anSocial.sendRequest("posts/query.json", AnSocialMethod.GET,
params, new IAnSocialCallback() {
@Override
public void onSuccess(JSONObject response) {
try {
JSONObject meta = response.getJSONObject("meta");
int total =meta.getInt("total");
JSONArray postsJson = response.getJSONObject("response")
.getJSONArray("posts");
// 如果服务器的post大于0
if (total > 0) {
// post存入数据库,并加入tempPostList
savePostToDB(postsJson);
// 根据postId到服务器查询Like和Comment
loadLikeAndCommentByPostId();
}
//如果服务器返回的post等于0,界面展示没有更多朋友圈消息
else {
//showToast("找不到更多的朋友圈消息啦");
}
} catch (JSONExceptione) {
}
}
@Override
public void onFailure(JSONObject response) {
}
});
} catch(ArrownockException e) {
}
}
}


如何在App中实现朋友圈功能系列文章:之一朋友圈实现原理浅析之二快速实现用户信息的自定义之三快速实现双向好友功能之四在朋友圈中添加发送图片功能之五点赞、评论属性详细解析之六快速实现下拉加载朋友圈功能之七快速实现上拉加载朋友圈功能之八页面加载功能的逻辑与实现

本文出自 “箭扣科技Arrownock” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: