本文来自网易云社区
作者:汪昕濛
Map params = new HashMap();
params.put("from", senderAccount.getAccid());
params.put("to", receiverAccount.getAccid());
params.put("limit", "100");
params.put("reverse", "2");
long now = System.currentTimeMillis();
params.put("begintime", String.valueOf(now-21 * 24 * 3600 * 1000));
params.put("endtime", String.valueOf(now));
Map params = new HashMap();
params.put("from", senderAccount.getAccid());
params.put("to", receiverAccount.getAccid());
params.put("limit", "100");
params.put("reverse", "2");
long now = System.currentTimeMillis();
params.put("begintime", String.valueOf(now-31 * 24 * 3600 * 1000));
params.put("endtime", String.valueOf(now));
开发小哥哥诚不欺我,果然报错了。返回的错误信息如下:{"desc":"bad time","code":414}
String endTime = String.valueOf(System.currentTimeMillis());
JSONArray msgs = new JSONArray();
Map params = new HashMap();
params.put("from", senderAccount.getAccid());
params.put("to", receiverAccount.getAccid());
params.put("limit", "100");
params.put("reverse", "2");//按时间倒序返回
params.put("begintime", "0");
while(true) {
params.put("endtime", endTime);
String resposeContent = RequestUtil.doPostRequest("https://api.netease.im/nimserver/history/querySessionMsg.action", params, makeYunXinHeaderMap(), true);
if(StringUtils.isBlank(resposeContent)) {
break;
}
JSONObject result = JSONObject.fromObject(resposeContent);
if(result.getInt("code") != HttpStatus.SC_OK) {
logger.info("query yunxin message history error, result:"+resposeContent+" & "+senderId + " to " + receiverId);
break;
}
JSONArray subMsgs = result.getJSONArray("msgs");
msgs.addAll(subMsgs);
//少于100条记录说明历史记录已全部返回,不用分批次再查询
if(subMsgs.size() < 100) {
break;
}
endTime = subMsgs.getJSONObject(subMsgs.size() - 1).getString("sendtime");
}
return msgs;
网易云免费体验馆,0成本体验20+款云产品!
更多网易研发、产品、运营经验分享请访问网易云社区。