First, ring letter instant Cloud Introduction
Website: http://docs.easemob.com/doku.php?id=start:100serverintegration:30chatlog
This project uses the "Android client" to chat with experts and users, and the chat content is stored in a JSON format in the ring-mail instant Cloud database. The format is roughly as follows:
Second, JavaBean design:
Because the actual chat content in the ring letter is a three-layer nested pattern, it is necessary to establish a three-level javabean to parse and save the data, which I have designed here in the following format:
Third, the code implementation:
1. Package Structure:
The project uses the Jeesite framework, which is essentially SPRINGMVC + Mybatis, and incorporates Shiro and Activiti content. Perhaps because of the particularity of the Jeesite architecture, it is necessary to build packages according to the MVC model, otherwise the Spring class scan will be invalidated and the service layer and DAO layer objects cannot be established. The package structure is as follows:
At the same time, the project needs to import the jar package of the ring letter, because the jar package contains some configuration-specific constants for the ring-letter user, which is called in the method. Otherwise, or the display does not have permission to download data. The code is as follows:
@Service public class hxdownloadmessageservice{private static final String APPKEY = Constants.appkey; /* * Use the app's client_id and Client_secret to get app Admin token */private static credential credential = new Clientsecretcredential
(constants.app_client_id, Constants.app_client_secret, roles.user_role_appadmin); public void Downloadhxmessage () {String Senvendayago = string.valueof (System.currenttimemillis ()-15 * 24 * 60 * 60 *
1000);
Objectnode queryStrNode1 = Factory.objectnode ();
Querystrnode1.put ("QL", "SELECT * where timestamp>" + Senvendayago);
Querystrnode1.put ("Limit", "1000");
Objectnode messages1 = getchatmessages (queryStrNode1);
Get all Chat History System.out.println ("================================================") of the ring letter;
System.out.println ("Ring letter json for all chat records:" + messages1.tostring ());
System.out.println ("================================================"); /* * Get the contents of the dialog entity entities in the Ring database, save in Entities Jsonarray */jsonobject jsonobj = Json.parseobjeCT (messages1.tostring ());
Jsonarray entities = Jsonobj.getjsonarray ("entities");
list
/** * Get HX JSON Chat message * * @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"). AsT
Ext (); } 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);
System.out.println ("Access URL:" +chatmessagesurl.tostring ());
Objectnode = Httpclientutils.sendhttprequest (chatmessagesurl, credential, NULL, httpmethod.method_get);
} catch (Exception e) {e.printstacktrace ();
} return Objectnode; }
Parse the JSON data into the Hxmessentity collection by Alibaba-fastjson.jar.