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

之六

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

 

实现逻辑:

根据上次下拉刷新的时间从数据库加载出limit(比如10)条post,判断总数据条数:

a. 如果总数据大于limit条,清空当前缓存的postList,将服务器获取的post存入数据库,根据本地数据库里的最新一条like的created_at和最新一条comment的created_at作为begin_time去服务器获取like和comment,全部获取完成后,从数据库取出10条post以及对应的like和comment,最后刷新界面。

b. 如果总数据大于0,小于等于limit,则不需要清空当前缓存的postList,而是直接到服务器取like和comment的数据,全部获取完成后,将对应的like和comment添加到缓存的postList和服务器获取的post中,再把从服务器获取的post按顺序加载到缓存的postList中,最后刷新界面。

c. 如果总数据等于0,判断当前缓存postList的是否为空,如果为空,那么什么都不做。如果不为空,那么就获取comment和like,全部加载好后,刷新界面。

 

查询逻辑实现(以Android系统为例):


    //下拉加载朋友圈
    private void pullToQueryPost() {
        // 根据上次上拉的时间从服务器获取post
        Map<String, Object>params = loadPullToQueryPostParams();
        try {
            anSocial.sendRequest("posts/query.json", AnSocialMethod.GET, params,
                new IAnSocialCallback() {
                @Override
                public void onSuccess(JSONObject response) {
                    try {
                        JSONObjectmeta = response.getJSONObject("meta");
                        int total =meta.getInt("total");
                        JSONArraypostsJson = response.getJSONObject("response")
                            .getJSONArray("posts");
                        // 如果post的数量大于limit
                        if (total > POST_LIMIT) {
                            // 存入数据库
                            savePostToDB(postsJson);
                            //清空当前缓存的postList
                            postList.clear();
                            postList.addAll(tempPostList);
                            /*
                            根据本地数据库里的最新一条like的created_at和
                            最新一条comment的created_at作为begin_time
                            去服务器获取like和comment
                            */
                            loadLikeAndCommentByLatestData(true);
                        }
                        // 如果post的数量大于0,小于等于limit
                        else if (total <= POST_LIMIT&& total > 0) {
                            // 存入数据库
                            savePostToDB(postsJson);
                            postList.addAll(0, tempPostList);
                            /*
                            根据本地数据库里的最新一条like的created_at和
                            最新一条comment的created_at
                            作为begin_time去服务器获取like和comment*/
                            loadLikeAndCommentByLatestData(true);
                       }
                       // 如果post的数量等于0
                       else {
                            // 如果size等于0,刷新界面
                            if (postList.isEmpty()) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mPullRefreshListView.onRefreshComplete();
                                    }
                                });
                            }
                            // 如果size不等于0
                            else {
                                /*
                                根据本地数据库里的最新一条like的created_at和
                                最新一条comment的created_at
                                作为 begin_time去服务器获取like和comment
                                */
                                loadLikeAndCommentByLatestData(true);
                            }
                        }
                    } catch (JSONException e) {
                        showToast("查询朋友圈消息失败了呢");
                        Log.e("queryPost", "queryPost==JSONException");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                            mPullRefreshListView.onRefreshComplete();
                            }
                        });
                    }
                } 
                @Override
                public void onFailure(JSONObject response) {
                    showToast("查询朋友圈消息失败了呢");
                    Log.e("queryPost", "queryPost==" + response);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mPullRefreshListView.onRefreshComplete();
                        }
                    });
                }
            });
        } catch(ArrownockException e) {
            showToast("查询朋友圈消息失败了呢");
            Log.e("queryPost", "queryPost==ArrownockException");
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mPullRefreshListView.onRefreshComplete();
                }
            });
        }
    }



参数组装:

其中根据上次下拉刷新的时间加载post,实际上就是每次刷新时,往本地缓存例插入了一条记录,下次刷新时取出来。

如下是组成到服务器查询Post参数的方法代码:


    // 组成查询Post参数
    private Map<String, Object>loadPullToQueryPostParams() {
        Map<String, Object>params = new HashMap<String, Object>();
        List<User> userList =UserHelper.getAllUsers();
        StringBuilder userIdBuilder= new StringBuilder();
        for (User u : userList) {
            userIdBuilder.append(u.userId).append(",");
        }
        String user_id =userIdBuilder.substring(0, userIdBuilder.length() - 1);
        params.put("wall_id", getString(R.string.wall_id)); //wall_id
        params.put("user_id", user_id); // 所有好友的user_id
        long pullTime = SystemMap.getLong(ImppConstants.
            POST_FRIEND_PULL_TIME, 0); //得到上次下拉刷新的时间
        long newPullTime = CommonUtils.getTimeForChinaTimeZone();
        SystemMap.setLong(ImppConstants.POST_FRIEND_PULL_TIME
            , newPullTime); // 将当前时间存入缓存
        if (0 != pullTime) {
            params.put("begin_time", pullTime + 1);
        }
        params.put("sort", "-created_at"); //根据created_at倒叙排序
        params.put("limit", POST_LIMIT);
        return params;
    }




如何在App中实现朋友圈功能系列文章:

之一朋友圈实现原理浅析

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

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

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

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

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

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

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