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


如何在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 //墙Id

user_id //用户的所有好友id,多个好友id用”,”分割

sort=-created_at //根据Post的创建时间倒序排序,取最新的Post

limit //自定义取多少条数据

查询Comment和Like,使用comments/query.json和Likes/query.json参数: object_type=Post //对象类型,固定Post

object_id //postId

user_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中实现朋友圈功能系列文章:

之一朋友圈实现原理浅析

之二快速实现用户信息的自定义

之三快速实现双向好友功能

之四在朋友圈中添加发送图片功能

之五点赞、评论属性详细解析

之六快速实现下拉加载朋友圈功能

之七快速实现上拉加载朋友圈功能

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值