注册

分页获取聊天记录的结果条数不对

String currentTimestamp = String.valueOf(System.currentTimeMillis());
        ObjectNode queryStrNode2 = factory.objectNode();
        queryStrNode2.put("ql", "select * where timestamp<" + currentTimestamp);
        queryStrNode2.put("limit", "10");
        // 第一页
        ObjectNode messages2 = getChatMessages(queryStrNode2);
        System.out.println("-------------"+messages2);
        // 第二页
        String cursor = messages2.get("cursor").asText();
        queryStrNode2.put("cursor", cursor);
        ObjectNode messages3 = getChatMessages(queryStrNode2);
        System.out.println("+++++++++++++"+messages3);
 
第一页返回的数据条数count是9,第二页返回的数据条数count是10,为啥会不一样?
已邀请:

beyond - imgeek运营

这和分页没关系把,上面是写死了limit=10 ,每页去替换cursor
把第一页的数据发出来看一下呢,主要是看
 "limit": [
            "10"
        ]
 "count": 10
这里
确定写的limit 10 ,而返回来 "count": 是9
以下几种情况每次返回的记录都是9条:1、String currentTimestamp = String.valueOf(System.currentTimeMillis());
        ObjectNode queryStrNode2 = factory.objectNode();
        queryStrNode2.put("ql", "select * where timestamp<" + currentTimestamp);
        queryStrNode2.put("limit", "10");
2、String currentTimestamp = String.valueOf(System.currentTimeMillis());
        ObjectNode queryStrNode2 = factory.objectNode();
        queryStrNode2.put("limit", "10");
3、String currentTimestamp = String.valueOf(System.currentTimeMillis());
        ObjectNode queryStrNode2 = factory.objectNode();
        queryStrNode2.put("ql", "select * where timestamp<" + currentTimestamp);
4、String currentTimestamp = String.valueOf(System.currentTimeMillis());
        ObjectNode queryStrNode2 = factory.objectNode();
       
 
{"action":"get","params":{"limit":["10"],"ql":["select * where timestamp<1442556622244"]},"path":"/chatmessages","uri":"http://a1.easemob.com/neweye/6516516/chatmessages","entities":[{"uuid":"b8a19dda-be2e-11e4-8e7e-2305cf6568e1","type":"chatmessage","created":1425006860333,"modified":1425006860333,"timestamp":1425006859604,"from":"admin","msg_id":"14256859603909","to":"111","chat_type":"chat","payload":{"bodies":[{"type":"txt","msg":"你好"}]}},{"uuid":"b8a1c4ea-be2e-11e4-b070-91972802aec1","type":"chatmessage","created":1425006860334,"modified":1425006860334,"timestamp":1425006859609,"from":"admin","msg_id":"14256859606903","to":"111","chat_type":"chat","payload":{"bodies":[{"type":"img","filename":"C:\\fakepath\\QQ截图20150120163917.png","secret":"tPIDWr4uEeS2WsFFiYA_oXoo9yJNqmZnRKscucqTJFlLNRbs","url":"https://a1.easemob.com/neweye/6516516/chatfiles/b4f20350-be2e-11e4-92b2-9fbabe0c44c5"}]}},{"uuid":"610315da-be2f-11e4-a586-3fc36b8c581f","type":"chatmessage","created":1425007142829,"modified":1425007142829,"timestamp":1425007141871,"from":"222","msg_id":"31964614853919188_WEBIM_2543ef72f4","to":"111","chat_type":"chat","payload":{"bodies":[{"type":"txt","msg":"[):]"}],"ext":{}}},{"uuid":"666e431e-be2f-11e4-9fc0-0fcd4af4639a","type":"chatmessage","created":1425007151920,"modified":1425007151920,"timestamp":1425007151822,"from":"222","msg_id":"31964657598071252_WEBIM_2543ef99d5","to":"14256971336011","groupId":"14256971336011","chat_type":"groupchat","payload":{"bodies":[{"type":"txt","msg":"[(D)]"}],"ext":{}}},{"uuid":"6c7031fa-be2f-11e4-81a7-b3398bbb78cb","type":"chatmessage","created":1425007161999,"modified":1425007161999,"timestamp":1425007161059,"from":"111","msg_id":"31964697347490280_WEBIM_2543efbddf","to":"222","chat_type":"chat","payload":{"bodies":[{"type":"txt","msg":"[({)]"}],"ext":{}}},{"uuid":"6edb3e8a-be2f-11e4-a7a2-5d77f4da4283","type":"chatmessage","created":1425007166056,"modified":1425007166056,"timestamp":1425007166010,"from":"111","msg_id":"31964718616805864_WEBIM_2543efd11d","to":"222","chat_type":"chat","payload":{"bodies":[{"type":"txt","msg":"[:(]"}],"ext":{}}},{"uuid":"7913843e-be2f-11e4-b219-4bd1616260db","type":"chatmessage","created":1425007183202,"modified":1425007183202,"timestamp":1425007182868,"from":"222","msg_id":"31964790930801108_WEBIM_2543f012e5","to":"14256971336011","groupId":"14256971336011","chat_type":"groupchat","payload":{"bodies":[{"type":"txt","msg":"[(R)]"}],"ext":{}}},{"uuid":"7da7794a-be31-11e4-aba8-d543fc793d7b","type":"chatmessage","created":1425008049876,"modified":1425008049876,"timestamp":1425008049644,"from":"111","msg_id":"31968513727922644_WEBIM_2543fd4ce7","to":"14256971336011","groupId":"14256971336011","chat_type":"groupchat","payload":{"bodies":[{"type":"txt","msg":"[(*)]"}],"ext":{}}},{"uuid":"8570458a-be31-11e4-8571-c9676a1ccb01","type":"chatmessage","created":1425008062936,"modified":1425008062936,"timestamp":1425008061942,"from":"333","msg_id":"31968566517432788_WEBIM_2543fd7d06","to":"111","chat_type":"chat","payload":{"bodies":[{"type":"txt","msg":"[(H)]"}],"ext":{}}}],"timestamp":1442556621615,"duration":3063,"cursor":"LTIxNjQ2Mzg3MjpnR2tBQVFNQWdHa0FCZ0ZMeVJsdDlnQ0FkUUFRaFhCRmlyNHhFZVNGY2NsbmFoekxBUUNBZFFBUWhYQkZnTDR4RWVTX3FnY2dwcDZBZmdB","count":9,"statusCode":200}
/**
 * REST API Demo : 聊天消息导出REST API HttpClient4.3实现
 * 
 * Doc URL: http://www.easemob.com/docs/rest/chatmessage/
 * 
 * @author Lynch 2014-09-15
 * 
 */
public class EasemobChatMessage {

    private static final Logger LOGGER = LoggerFactory.getLogger(EasemobChatMessage.class);
    private static final JsonNodeFactory factory = new JsonNodeFactory(false);
 private static final String APPKEY = Constants.APPKEY;


    // 通过app的client_id和client_secret来获取app管理员token
    private static Credential credential = new ClientSecretCredential(Constants.APP_CLIENT_ID,
            Constants.APP_CLIENT_SECRET, Roles.USER_ROLE_APPADMIN);

    /**
     * Main Test
     *
     * @param args
     */
    public static void main(String args) {

        // 聊天消息 获取最新的20条记录
//        ObjectNode queryStrNode = factory.objectNode();
//        queryStrNode.put("ql", "select+*+where+from='111'+and+to='333'");
//        queryStrNode.put("limit", "10");
//        queryStrNode.put("cursor", "NjQzMjIwMzYyOmdHa0FBUUVBZ0hNQUF6RXhNUUNBZFFBUTR4d2JDc0NKRWVTNkRPWEk2NEhHeWdDQWRRQVE0eHdiQU1DSkVlU3pzZVZEYVhnMnZRQQ");
//        ObjectNode messages = getChatMessages(queryStrNode);
//        System.out.println("=====================================" + messages);
        
        // 聊天消息 获取7天以内的消息
//        String currentTimestamp = String.valueOf(System.currentTimeMillis());
//        String senvenDayAgo = String.valueOf(System.currentTimeMillis() - 365 * 24 * 60 * 60 * 1000);
//        ObjectNode queryStrNode1 = factory.objectNode();
//        queryStrNode1.put("ql", "select * where timestamp>" + "1425011630720" + " and timestamp<" + currentTimestamp);
        //?ql=select * where timestamp>1403164734226&limit=10&cursor=**
//        queryStrNode1.put("ql", "select * where timestamp<" + currentTimestamp);
//        queryStrNode1.put("limit", "1000");
//        ObjectNode messages1 = getChatMessages(queryStrNode1);
//        System.out.println("=====================================" + messages1);
        
        // 聊天消息 分页获取
        String currentTimestamp = String.valueOf(System.currentTimeMillis());
        ObjectNode queryStrNode2 = factory.objectNode();
        queryStrNode2.put("ql", "select * where timestamp<" + currentTimestamp);
//        queryStrNode2.put("ql", "select+*+where+from='111'+and+to='333'");
        queryStrNode2.put("limit", "10");
        // 第一页
        ObjectNode messages2 = getChatMessages(queryStrNode2);
        System.out.println("-------------"+messages2);
//        // 第二页
//        String cursor = messages2.get("cursor").asText();
//        queryStrNode2.put("cursor", cursor);
//        ObjectNode messages3 = getChatMessages(queryStrNode2);
//        System.out.println("+++++++++++++"+messages3);
    }

    /**
     * 获取聊天消息
     * 
     * @param queryStrNode
     *
     */
    public static ObjectNode getChatMessages(ObjectNode queryStrNode) {

        ObjectNode objectNode = factory.objectNode();

        // check appKey format
        if (!HTTPClientUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
            LOGGER.error("Bad format of Appkey: " + APPKEY);

            objectNode.put("message", "Bad format of Appkey");

            return objectNode;
        }

        try {

            String rest = "";
            if (null != queryStrNode && queryStrNode.get("ql") != null && !StringUtils.isEmpty(queryStrNode.get("ql").asText())) {
                rest = "ql="+ java.net.URLEncoder.encode(queryStrNode.get("ql").asText(), "utf-8");
            }
            if (null != queryStrNode && queryStrNode.get("limit") != null && !StringUtils.isEmpty(queryStrNode.get("limit").asText())) {
                rest = rest + "&limit=" + queryStrNode.get("limit").asText();
            }
            if (null != queryStrNode && queryStrNode.get("cursor") != null && !StringUtils.isEmpty(queryStrNode.get("cursor").asText())) {
                rest = rest + "&cursor=" + queryStrNode.get("cursor").asText();
            }
        
//            URL chatMessagesUrl = HTTPClientUtils.getURL(Constants.APPKEY.replace("#", "/") + "/chatmessages?" + rest);
            URL chatMessagesUrl = HTTPClientUtils.getURL(Constants.APPKEY.replace("#", "/") + "/chatmessages?" + rest);
            
            objectNode = HTTPClientUtils.sendHTTPRequest(chatMessagesUrl, credential, null, HTTPMethod.METHOD_GET);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return objectNode;
    }

}

要回复问题请先登录注册